hdu 5936 2016ccpc 杭州 - D
数学题好难啊!!!!
最长长度不超过十位, 折半枚举。。。
1 #include<bits/stdc++.h> 2 #define LL long long 3 #define fi first 4 #define se second 5 #define mk make_pair 6 #define pii pair<int,int> 7 #define piii pair<int, pair<int,int>> 8 9 using namespace std; 10 11 const int N = 1e5 + 7; 12 const int M = 1e4 + 7; 13 const int inf = 0x3f3f3f3f; 14 const LL INF = 0x3f3f3f3f3f3f3f3f; 15 const LL base = 100000; 16 const int mod = 1e9 + 7; 17 18 int x, k; 19 LL f[N][10], v[N]; 20 21 int fastPow(int a, int b) { 22 int ans = 1; 23 while(b) { 24 if(b & 1) ans = ans * a; 25 a = a * a; b >>= 1; 26 } 27 return ans; 28 } 29 void init() { 30 for(int i = 0; i < N; i++) { 31 for(int j = 0; j < 10; j++) { 32 for(int now = i; now; now /= 10) { 33 f[i][j] += fastPow(now % 10, j); 34 } 35 } 36 } 37 } 38 39 40 int main() { 41 init(); 42 int T; scanf("%d", &T); 43 for(int cas = 1; cas <= T; cas++) { 44 scanf("%d%d", &x, &k); 45 LL ans = 0; 46 for(int i = 0; i < base; i++) 47 v[i] = f[i][k] - i * base; 48 sort(v, v + base); 49 50 for(int i = 0; i < base; i++) { 51 LL ret = f[i][k] - i; 52 ans += upper_bound(v, v + base, x - ret) - lower_bound(v, v + base, x - ret); 53 } 54 55 ans -= (x == 0); 56 printf("Case #%d: ", cas); 57 printf("%lld\n", ans); 58 } 59 return 0; 60 } 61 /* 62 */