标签:miss signed java msu dig war other return field
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 256000/256000 K (Java/Others)
Total Submission(s): 559 Accepted Submission(s): 299
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 typedef long long LL; 5 const int maxn = 1010; 6 const int maxm = 55; 7 const LL mod = 1e9+7; 8 int digit[maxn], revert[maxn]; 9 LL L, R, l, r; 10 LL dp[maxm][maxm][maxn][2]; 11 12 LL dfs(int k, int s, int l, bool ok, bool lim) { 13 if(l < 0) { 14 if(ok) return k; 15 return 1; 16 } 17 if(!lim && ~dp[k][s][l][ok]) return dp[k][s][l][ok]; 18 int pos = lim ? digit[l] : k - 1; 19 LL ret = 0; 20 for(int i = 0; i <= pos; i++) { 21 revert[l] = i; 22 if(i == 0 && s == l) { 23 ret += dfs(k, s-1, l-1, ok, lim&&(i==pos)); 24 } 25 else if(ok && l < (s + 1) / 2) { 26 ret += dfs(k, s, l-1, i==revert[s-l], lim&&(i==pos)); 27 } 28 else { 29 ret += dfs(k, s, l-1, ok, lim&&(i==pos)); 30 } 31 } 32 if(!lim) dp[k][s][l][ok] = ret; 33 return ret; 34 } 35 36 LL f(LL n, LL k) { 37 if(n == 0) return k; 38 int pos = 0; 39 while(n) { 40 digit[pos++] = n % k; 41 n /= k; 42 } 43 return dfs(k, pos-1, pos-1, 1, 1); 44 } 45 46 47 signed main() { 48 int T, tt = 1; 49 scanf("%d", &T); 50 memset(dp, -1, sizeof(dp)); 51 while(T--) { 52 scanf("%lld%lld%lld%lld",&L,&R,&l,&r); 53 LL ret = 0; 54 for(int i = l; i <= r; i++) { 55 ret += f(R, i) - f(L-1, i); 56 } 57 printf("Case #%d: %lld\n", tt++, ret); 58 } 59 return 0; 60 }
标签:miss signed java msu dig war other return field
原文地址:http://www.cnblogs.com/mj-liylho/p/7400480.html