2016弱校联盟十一专场10.5(12点场) Increasing or Decreasing bnu 52325
2016-10-05 18:49
435 查看
#pragma comment(linker, "/STACK:102400000,102400000") #include <algorithm> #include <iostream> #include <iomanip> #include <sstream> #include <cstring> #include <cstdio> #include <string> #include <vector> #include <bitset> #include <queue> #include <stack> #include <cmath> #include <ctime> #include <list> #include <set> #include <map> #include <queue> #define Pi acos(-1.0) using namespace std; typedef long long ll; ll DP[22][11][5][3]; int DIG[22]; // type 1 不减 2 不加 ll ddp[11][22][5]; void getddp(){ memset(ddp, 0, sizeof(ddp)); for (int i = 0; i < 10; i++){ for (int j = 0; j < 3; j++){ ddp[i][0][j] = 1; } } for (int i = 0; i < 19; i++){ ddp[9][i][1] = 1; } for (int i = 8; i >= 0; i--){ for (int j = 1; j < 19; j++){ for (int k = i; k < 10; k++){ ddp[i][j][1] += ddp[k][j-1][1]; } } } for(int i = 0;i <= 9;i++){ for(int k = 0;k < 19;k++){ ddp[9-i][k][2] = ddp[i][k][1]; } } for(int i = 0;i <= 9;i++){ for(int k = 0;k < 19;k++){ ddp[i][k][0] = ddp[i][k][1] + ddp[i][k][2] - 1; } } /* for (int i = 0; i < 19; i++){ ddp[0][i][2] = 1; } for (int i = 1; i <= 9; i++){ for (int j = 1; j < 19; j++){ for (int k = i; k > 0; k--){ ddp[i][j][1] += ddp[k][j-1][1]; } } }*/ } int dfs(int pos,int pre,int limit,int type,int judge) { if(pos < 0) return 1; if(!limit && DP[pos][pre][type][judge] != -1) return DP[pos][pre][type][judge]; int end = limit ? DIG[pos] : 9; int ret = 0; if(judge == 0){ // printf("?"); //ret += ddp[0][pos][1] + ddp[0][pos][2] - 1; ret += dfs(pos-1,0,0,type,0); for(int i = 1;i <= end;i++){ ret += ddp[i][pos][1] + ddp[i][pos][2] - 1; // ret += dfs(pos-1,i,0,type,1); } } else{ for(int i = 0;i <= end;i++){ if(type==1 && i <pre) continue; if(type==2 && i >pre) continue; if(type == 0) { int ntype; if(i == pre){ ntype = type; } else if(i > pre){ ntype = 1; } else{ ntype = 2; } if(limit) ret += dfs(pos-1,i,limit && (i == end),ntype,1); else{ ret += ddp[i][pos][ntype]; } } else{ if(limit) ret += dfs(pos-1,i,limit && (i == end),type,1); else{ ret += ddp[i][pos][type]; } } } } if(!limit) DP[pos][pre][type][judge] = ret; return ret; } ll solove(ll x){ int num = 0; ll tem = x; memset(DP,-1,sizeof DP); while(x){ DIG[num] = x % 10; num++; x/=10; } ll ans = 0; ans += dfs(num-2,0,0,0,0); for(int i = 1;i <= DIG[num-1];i++){ // printf("ans = %lld\n" , ans); ans += dfs(num-2,i,i==DIG[num-1],0,1); } return ans; } int main(){ ll l,r; int m; scanf("%d", &m); getddp(); while(m--){ scanf("%lld%lld",&l, &r); printf("%lld\n", solove(r)-solove(l-1)); } }
数位DP
相关文章推荐
- 2016弱校联盟十一专场10.5(12点场) Increasing or Decreasing
- 2016弱校联盟十一专场10.5 I Increasing or Decreasing(数位dp)
- 2016弱校联盟十一专场10.3(12点场)
- 2016弱校联盟十一专场10.5---As Easy As Possible(倍增)
- BNUOJ 52325 Increasing or Decreasing 数位dp
- POJ 3735 Training little cats(矩阵乘法 + 稀疏矩阵优化)——2016弱校联盟十一专场10.7(12点场)
- 2016弱校联盟十一专场10.5 F Fibonacci of Fibonacci(矩阵快速幂+找循环节)
- 2016弱校联盟十一专场10.5 F Fibonacci of Fibonacci(矩阵快速幂 + 循环节)
- [弱校联萌2016]2016弱校联盟十一专场10.5
- 弱校联萌 BNU I. Increasing or Decreasing
- 2016弱校联盟十一专场10.5---As Easy As Possible(倍增)
- 2016弱校联盟十一专场10.7(12点场)-D. Blocks
- 2016弱校联盟十一专场10.2 Longest Increasing Subsequence
- 2016弱校联盟十一专场10.7(12点场)-M. Subimage Recognition
- 2016弱校联盟十一专场10.2 Longest Increasing Subsequence
- 2016弱校联盟十一专场10.7(12点场) E. Training little cats
- (2016弱校联盟十一专场10.5) F. Fibonacci of Fibonacci
- [Contests]2016弱校联盟十一专场10.7
- [弱校联萌2016]2016弱校联盟十一专场10.3
- Problem I. Increasing or Decreasing MIPT-2016 Pre-Finals Workshop, Taiwan NTU Contest, Sunday, March 27, 2016