Little Ruins is playing a number game, first he chooses two positive integers
y
y and
K
K and calculates
f(y,K)
f(y,K), here
then he gets the result
As Ruins is forgetful, a few seconds later, he only remembers K K, x x and forgets y y. please help him find how many y y satisfy x=f(y,K)−y x=f(y,K)−y.
f(y,K)=∑z in every digits of yzK(f(233,2)=22+32+32=22)
f(y,K)=∑z in every digits of yzK(f(233,2)=22+32+32=22)
then he gets the result
x=f(y,K)−y
x=f(y,K)−y
As Ruins is forgetful, a few seconds later, he only remembers K K, x x and forgets y y. please help him find how many y y satisfy x=f(y,K)−y x=f(y,K)−y.
Every test case contains one line with two integers x x, K K.
Limits
1≤T≤100 1≤T≤100
0≤x≤109 0≤x≤109
1≤K≤9 1≤K≤9
2 2 2 3 2
Case #1: 1Case #2: 2
#include<iostream> using namespace std; #include<algorithm> #include<cstring> #include<cstdlib> #include<cstdio> #include<map> #define LL long long const int e[]={1, 10, 100, 1000, 10000, 100000}; int mi[13][13]; map<LL,int>coun[10]; void init() { for(int i=0;i<10;i++){ mi[0][i]=1; for(int j=1;j<10;j++)mi[j][i]=mi[j-1][i]*i; } for(int i=1;i<10;i++) { for(int j=0;j<e[5];j++) { LL sum=-j; for(int t=0;t<=4;++t) sum+=mi[i][j/e[t]%10]; coun[i][sum]++; } } } int main() { init(); int T; scanf("%d",&T); for(int k=1; k<=T; k++) { int x,b; LL ans=0; scanf("%d%d",&x,&b); for(int i=0;i<100000;i++) { LL c,d; d=-(LL)i*100000; for(int j=0;j<=4;++j) d+=mi[b][i/e[j]%10]; c=x-d; if(coun[b].find(c)!=coun[b].end())//这里画重点,不知道为什么加就对,不加就不对, ans=ans+coun[b][c]; //如果知道可以评论。 } if(x==0) ans--; printf("Case #%d: %lld\n",k,ans); } return 0; }