Difference HDU - 5936

Little Ruins is playing a number game, first he chooses two positive integers yy and KK and calculates f(y,K)f(y,K), here 

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)yx=f(y,K)−y


As Ruins is forgetful, a few seconds later, he only remembers KKxx and forgets yy. please help him find how many yy satisfy x=f(y,K)yx=f(y,K)−y.
InputFirst line contains an integer TT, which indicates the number of test cases. 

Every test case contains one line with two integers xxKK

Limits 
1T1001≤T≤100 
0x1090≤x≤109 
1K91≤K≤9 OutputFor every test case, you should output 'Case #x: y', where x indicates the case number and counts from 1 and y is the result. Sample Input
2
2 2
3 2
Sample Output
Case #1: 1

Case #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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章