hdu5389 Zero Escape(DP)
来源:互联网 发布:怎么减少网络延迟 编辑:程序博客网 时间:2024/05/20 17:59
点击打开链接
题目大意:有n个人,每个人都有个号码,有两个门a和b,现在要使得所有人都通过门(任意一个),进门条件是进入的所有人的数根等于a或者b,求方案数。
解题思路:一个整数的数根公式为:(x-1)%9+1.
用DP做:dp[i][j]表示前i个数选出来数根为j的方案数,状态转移方程也很容易推导,详情看代码,这里不做解释。
这里我们只要考虑n个人的号码组成a有多少种就可以了,因为剩下的都去b了。
但是要考虑到全都去b的情况,故sum==b时加1.
当然也可以考虑n个人号码组成b有多少种,同样的道理。
/* ***********************************************┆ ┏┓ ┏┓ ┆┆┏┛┻━━━┛┻┓ ┆┆┃ ┃ ┆┆┃ ━ ┃ ┆┆┃ ┳┛ ┗┳ ┃ ┆┆┃ ┃ ┆┆┃ ┻ ┃ ┆┆┗━┓ 马 ┏━┛ ┆┆ ┃ 勒 ┃ ┆ ┆ ┃ 戈 ┗━━━┓ ┆┆ ┃ 壁 ┣┓┆┆ ┃ 的草泥马 ┏┛┆┆ ┗┓┓┏━┳┓┏┛ ┆┆ ┃┫┫ ┃┫┫ ┆┆ ┗┻┛ ┗┻┛ ┆************************************************ */#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <stack>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>using namespace std;#define rep(i,a,b) for (int i=(a),_ed=(b);i<=_ed;i++)#define per(i,a,b) for (int i=(b),_ed=(a);i>=_ed;i--)#define pb push_back#define mp make_pairconst int inf_int = 2e9;const long long inf_ll = 2e18;#define inf_add 0x3f3f3f3f#define mod 258280327#define LL long long#define ULL unsigned long long#define MS0(X) memset((X), 0, sizeof((X)))#define SelfType intSelfType Gcd(SelfType p,SelfType q){return q==0?p:Gcd(q,p%q);}SelfType Pow(SelfType p,SelfType q){SelfType ans=1;while(q){if(q&1)ans=ans*p;p=p*p;q>>=1;}return ans;}#define Sd(X) int (X); scanf("%d", &X)#define Sdd(X, Y) int X, Y; scanf("%d%d", &X, &Y)#define Sddd(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,rx=getchar();return ra*fh;}//#pragma comment(linker, "/STACK:102400000,102400000")int add(int a,int b){ return (a+b-1)%9 + 1;}int dp[100005][10];int c[100005];int main(){//freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);ios::sync_with_stdio(0);cin.tie(0);int n,a,b,t;t = read();while(t--) { n = read(),a = read(),b = read(); int sum = 0; for(int i=1;i<=n;i++) { c[i] = read(); sum = add(sum,c[i]); } if(sum != add(a,b)) { int ans = 0; if(sum == a)ans++; if(sum == b)ans++; printf("%d\n",ans); continue; } MS0(dp); dp[0][0] = 1; for(int i=1;i<=n;i++) { for(int j=0;j<=9;j++) { (dp[i][j] += dp[i-1][j]) %= mod; (dp[i][add(j,c[i])] += dp[i-1][j]) %= mod; } } if(sum == b)dp[n][a]++; //都去b门 printf("%d\n",dp[n][a]); /* //答案也可以为这个 if(sum == a)dp[n][b]++; printf("%d\n",dp[n][b]); */ }return 0;}
转移方程为:dp[i][j] = (dp[i-1][j] + dp[i-1][(j - C[i] + 9) % 9])
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int MAXN = (int)1e6 + 10;const int MOD = 258280327;int dp[MAXN][10];int A, B, C[MAXN];int n;int main() { int T; scanf("%d", &T); while(T--) { scanf("%d%d%d", &n, &A, &B); int sum = 0, ans = 0; for(int i = 1; i <= n; i++) { scanf("%d", &C[i]); sum += C[i]; } if((A + B) % 9 != sum % 9) { if(sum % 9 == A % 9) ans++; if(sum % 9 == B % 9) ans++; printf("%d\n", ans); continue; } dp[0][0] = 1; for(int i = 1; i <= n; i++) { for(int j = 0; j < 9; j++) { dp[i][j] = (dp[i-1][j] + dp[i-1][(j - C[i] + 9) % 9]) % MOD; } } printf("%d\n", dp[n][A % 9]); } return 0;}
0 0
- hdu5389 Zero Escape(DP)
- HDU5389:Zero Escape(dp & 类背包)
- hdu5389 Zero Escape(模拟+找规律+DP)
- hdu5389(2015多校8)--Zero Escape(dp)
- HDU5389.Zero Escape
- hdu5389 Zero Escape
- hdu5389 Zero Escape
- hdu5389 Zero Escape(动态规划)
- hdu5389 Zero Escape DP+滚动数组 多校联合第八场
- hdu 5389 Zero Escape(dp)
- hdu 5389 Zero Escape(dp)
- 计数DP(Zero Escape,HDU 5389)
- hdu 5389 Zero Escape(dp)
- HDOJ 5389 Zero Escape DP
- hdu 5389 Zero Escape dp
- hdu 5389 Zero Escape (dp)
- hdu 5389 Zero Escape (dp)
- hdu5389(DP)
- SQL Server数据库查询速度慢原因及优化方法
- ArrrayList 和 LinkedList 查询效率区别举例说明
- yum源不存在解决方法
- java回忆录—输入输出流详细讲解(入门经典)
- 多项式求解(霍纳规则(Horner Rule))
- hdu5389 Zero Escape(DP)
- HDU:1231 最大连续子序列(动态规划DP)
- Java 7的ThreadLocalRandom与Random
- UVA - 1587 Box
- sql server中解决与系统保留关键字冲突使用的是中括号
- [设计模式]面向对象的六大原则
- UVA - 10110 Light, more light
- 数据结构练习题1
- Solr--全文索引原理