Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 256000/256000 K (Java/Others)
Total Submission(s): 863 Accepted Submission(s): 476
php
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <cstring> 5 #include <algorithm> 6 #include <set> 7 #include <map> 8 #include <queue> 9 #include <stack> 10 #include <vector> 11 using namespace std; 12 #define mod 1000000007 13 typedef long long ll; 14 int t; 15 int bit[40]; 16 int ans[40]; 17 ll dp[40][40][40]; 18 ll dfs(int pos,int zero,int jinzhi,int flag,int beg) 19 { 20 if(pos<0) return zero==0; 21 if(dp[pos][jinzhi][beg]!=-1&&!flag&&!zero) 22 return dp[pos][jinzhi][beg]; 23 ll sum=0; 24 int up=flag?bit[pos]:jinzhi-1; 25 for(int i=0; i<=up; i++){ 26 if(zero&&i==0) 27 sum+=dfs(pos-1,zero,jinzhi,flag&&i==up,beg); 28 else{ 29 if(zero){ 30 ans[pos]=i; 31 sum+=dfs(pos-1,0,jinzhi,flag&&i==up,pos); 32 } 33 else if(pos<(beg+1)/2){ 34 if(i==ans[beg-pos]) 35 sum+=dfs(pos-1,0,jinzhi,flag&&i==up,beg); 36 } 37 else{ 38 ans[pos]=i; 39 sum+=dfs(pos-1,0,jinzhi,flag&&i==up,beg); 40 } 41 } 42 } 43 ans[pos]=-1; 44 if(!flag&&!zero) 45 dp[pos][jinzhi][beg]=sum; 46 return sum; 47 } 48 ll slove (int x,int jinzhi){ 49 int len=0; 50 while(x) 51 { 52 bit[len++]=x%jinzhi; 53 x/=jinzhi; 54 } 55 return dfs(len-1,1,jinzhi,1,39); 56 } 57 int main() 58 { 59 scanf("%d",&t); 60 memset(dp,-1,sizeof(dp)); 61 int ce=1; 62 while(t--){ 63 int L,R,l,r; 64 scanf("%d %d %d %d",&L,&R,&l,&r); 65 ll ans=0; 66 for(int i=l; i<=r; i++){ 67 ll sum=slove(R,i)-slove(L-1,i); 68 ans=ans+sum*i+(R-L+1-sum); 69 } 70 printf("Case #%d: %lld\n",ce++,ans); 71 } 72 return 0; 73 }