题意:
我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字。
题目给出a,b,求出0~b有多少个不大于f(a)的数。
ac代码
1 #include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cstdio> 6 #include <vector> 7 #include <ctime> 8 #include <queue> 9 #include <list> 10 #include <set> 11 #include <map> 12 using namespace std; 13 #define INF 0x3f3f3f3f 14 typedef long long LL; 15 16 int bit[12], dp[12][60000]; 17 int t, A, B; 18 int solve(int len, int sum, int flag) 19 { 20 int res = 0; 21 if(sum < 0) 22 return 0; 23 if(len < 0) 24 return sum >= 0; 25 if(dp[len][sum] >= 0 && flag) 26 return dp[len][sum]; 27 int te = flag ? 9 : bit[len]; 28 for(int i = 0; i <= te; ++i) 29 { 30 int Sum = sum - i * (1 << len); 31 res += solve(len - 1, Sum, flag || i < te); 32 } 33 if(flag) 34 dp[len][sum] = res; 35 return res; 36 } 37 int main() 38 { 39 scanf("%d", &t); 40 memset(dp, -1, sizeof(dp)); 41 for(int i = 1; i <= t; ++i) 42 { 43 scanf("%d %d", &A, &B); 44 int len = 0; 45 while(B) 46 { 47 bit[len++] = B % 10; 48 B /= 10; 49 } 50 int temp = 0, j = 0; 51 while(A) 52 { 53 temp += (A % 10) * (1 << j); 54 j++; 55 A /= 10; 56 } 57 int res = solve(len-1, temp, 0); 58 printf("Case #%d: ", i); 59 printf("%d\n", res); 60 } 61 return 0; 62 }
wa代码,未解之谜
1 #include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cstdio> 6 #include <vector> 7 #include <ctime> 8 #include <queue> 9 #include <list> 10 #include <set> 11 #include <map> 12 using namespace std; 13 #define INF 0x3f3f3f3f 14 typedef long long LL; 15 16 int bit[12], dp[12][60000]; 17 int t, A, B; 18 int solve(int len, int sum, int flag) 19 { 20 int res = 0; 21 if(sum > A) 22 return 0; 23 if(len < 0) 24 return sum <= A; 25 if(dp[len][sum] >= 0 && flag) 26 return dp[len][sum]; 27 int te = flag ? 9 : bit[len]; 28 for(int i = 0; i <= te; ++i) 29 { 30 int Sum = sum + i * (1 << len); 31 res += solve(len - 1, Sum, flag || i < te); 32 } 33 if(flag) 34 dp[len][sum] = res; 35 return res; 36 } 37 int main() 38 { 39 scanf("%d", &t); 40 memset(dp, -1, sizeof(dp)); 41 for(int i = 1; i <= t; ++i) 42 { 43 scanf("%d %d", &A, &B); 44 int len = 0; 45 while(B) 46 { 47 bit[len++] = B % 10; 48 B /= 10; 49 } 50 int temp = 0, j = 0; 51 while(A) 52 { 53 temp += (A % 10) * (1 << j); 54 j++; 55 A /= 10; 56 } 57 A = temp; 58 int res = solve(len-1, 0, 0); 59 printf("Case #%d: ", i); 60 printf("%d\n", res); 61 } 62 return 0; 63 }