Codeforces 296B Yaroslav and Two Strings dp+容斥(入门
来源:互联网 发布:vue.js chrome插件 编辑:程序博客网 时间:2024/06/09 17:20
题目链接:点击打开链接
题意:
给出长度为n的2个数字串S ,T(有些位置为?表示可以随便填数字)
求:有多少种填充方式使得 S[i]>T[i] && S[j] <T[j]
思路:
先求出ans表示所有填充方式,ans = 10^num, num为2个串?的总个数
dp[0][i]表示长度为i 且对于任意的 j( 1<=j<=i)满足 S[j]<=T[j] 的填充方案数
dp[1][i] 表示 S[j]==T[j]
dp[2[i] 表示 S[j]>=T[j]
则答案= ans - dp[0][n] - dp[1][n] + dp[2][n];
#include<bits/stdc++.h>const int inf = 1e8;const double eps = 1e-8;const double pi = acos(-1.0);const int mod = 1e9+7;template <class T> inline bool rd(T &ret) { char c; int sgn; if(c=getchar(),c==EOF) return 0; while(c!='-'&&(c<'0'||c>'9')) c=getchar(); sgn=(c=='-')?-1:1; ret=(c=='-')?0:(c-'0'); while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0'); ret*=sgn; return 1; } template <class T> inline void pt(T x) { if (x <0) { putchar('-');x = -x; } if(x>9) pt(x/10); putchar(x%10+'0'); }using namespace std;typedef long long ll;typedef pair<int,int> pii;const int N = 1e5+10;int n;char s[2][N];ll dp[3][N];void mul(ll &x, ll y){ x = (x*y)%mod;}void add(ll &x, ll y){ x = (x+y)%mod;}int main(){ while(cin>>n){ scanf("%s", s[0]+1); scanf("%s", s[1]+1); ll ans = 1; for(int i = 0; i < 3; i++)dp[i][0] = 1; ll a, b, c; for(int i = 1; i <= n; i++){ if(s[0][i] == '?' && s[1][i] == '?') { a = c = 45; b = 10; mul(ans, 100); } else if(s[0][i] == '?') { mul(ans, 10); a = s[1][i]-'0'; b = 1; c = 9-a; } else if(s[1][i] == '?') { mul(ans, 10); c = s[0][i]-'0'; b = 1; a = 9-c; } else { a = s[0][i]<s[1][i]; b = s[0][i]==s[1][i]; c = s[0][i]>s[1][i]; } dp[0][i] = dp[0][i-1]*(a+b)%mod; dp[1][i] = dp[1][i-1]*b%mod; dp[2][i] = dp[2][i-1]*(b+c)%mod; } ans -= dp[0][n]; ans -= dp[2][n]; ans += dp[1][n]; ans %= mod; if(ans<0)add(ans, mod); pt(ans%mod); puts(""); } return 0;}
0 0
- Codeforces 296B Yaroslav and Two Strings dp+容斥(入门
- CodeForces 296B Yaroslav and Two Strings (容斥)
- CodeForces Round #179 (296B) - Yaroslav and Two Strings
- Codeforces Round #179 (Div. 2) B (codeforces 296b) Yaroslav and Two Strings
- Codeforces Round #179 (Div. 2) B. Yaroslav and Two Strings
- codeforces 301B. Yaroslav and Time
- Yaroslav and Time - CodeForces 301B Dijkstra
- CodeForces 301B Yaroslav and Time
- codeforces 223B Two strings
- codeforces-223B-Two Strings
- CodeForces 296A Yaroslav and Permutations
- 【Floyd】【CF】CodeForces 301B Yaroslav and Time
- codeforces 301B. Yaroslav and Time(下标的艺术)
- Codeforces 301B Yaroslav and Time 【最短路】
- CodeForces--301B--Yaroslav and Time(弗洛伊德)
- codeforces B. Mike and strings
- Codeforces 595 B. Pasha and Phone (容斥)
- codeforces 449D Jzzhu and Numbers 容斥+DP
- [POJ3922]Now解题报告
- Annotation笔记
- BZOJ 2152 (树形DP)
- oc中不允许方法重载
- 逆序对的求法 归并排序
- Codeforces 296B Yaroslav and Two Strings dp+容斥(入门
- Python函数小结(2)-- 装饰器、 lambda
- android 常用URI 值得记住
- 【leetcode】Valid Parentheses
- Android最佳实践之UI篇(20条UI最佳实践建议)
- 最短路径 Dijkstra算法
- Ansible的第一课:安装它
- sqlmap学习及代码简析
- 第十一讲_项目三_买玫瑰