Difference
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 764 Accepted Submission(s): 197
Problem Description
Little Ruins is playing a number game, first he chooses two positive integers
y
and
K
and calculates
f(y,K)
, here
then he gets the result
As Ruins is forgetful, a few seconds later, he only remembers K , x and forgets y . please help him find how many y satisfy x=f(y,K)−y .
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
As Ruins is forgetful, a few seconds later, he only remembers K , x and forgets y . please help him find how many y satisfy x=f(y,K)−y .
Input
First line contains an integer
T
, which indicates the number of test cases.
Every test case contains one line with two integers x , K .
Limits
1≤T≤100
0≤x≤109
1≤K≤9
Every test case contains one line with two integers x , K .
Limits
1≤T≤100
0≤x≤109
1≤K≤9
Output
For 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
Source
x = f(y,k) - y >= 0
-> f(y,k) >= y
-> y > 0
-> x > f(y,k) >= y
所以y的长度不超过10位
y = a + b*(1e5)
x = f(a,k) - a + f(b,k) - b*(1e5)
x - f(a,k) + a = f(b,k) - b*(1e5)
那f(b,k) - b*(1e5) == (x - f(a,k) + a)的数量和就是答案
预处理出所有的 f(b,k) - b*(1e5)
枚举a,二分法找出满足f(b,k) - b*(1e5) == (x - f(a,k) + a)的数量和即可
有坑!!!当x==0时,因为y>0,a,b不能同时为0,所以答案要-1
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<ctime>
#include<string.h>
#include<math.h>
#include<list>
using namespace std;
#define ll long long
#define pii pair<int,int>
const int inf = 1e9 + 7;
const int N = 1e5+5;
inline int read(){
int x;
char ch;
while(!isdigit(ch=getchar()));
x=ch-'0';
while(isdigit(ch=getchar())){
x=x*10+ch-'0';
}
return x;
}
ll f[N][10];
ll val[N];
ll qPow(int a,int n){
ll ans=1;
ll tmp=a;
while(n){
if(n&1){
ans*=tmp;
}
tmp*=tmp;
n>>=1;
}
return ans;
}
ll F(int y,int k){
ll ans=0;
while(y){
ans+=qPow(y%10,k);
y/=10;
}
return ans;
}
void Init(){
for(int i=0;i<N;++i){
for(int j=1;j<10;++j){
f[i][j]=F(i,j);
}
}
}
ll slove(ll x,int k){
const ll os=1e5;
for(int y=0;y<os;++y){
val[y]=f[y][k]-(ll)y*os;
}
sort(val,val+os);
ll ans=0;
for(int y=0;y<os;++y){
ll tmp=f[y][k]-y;
ans+=upper_bound(val,val+os,x-tmp)-lower_bound(val,val+os,x-tmp);
}
return ans-(x==0);
}
int main()
{
//freopen("/home/lu/Documents/r.txt","r",stdin);
//freopen("/home/lu/Documents/w.txt","w",stdout);
int k,T;
ll x;
scanf("%d",&T);
Init();
for(int t=1;t<=T;++t){
scanf("%lld%d",&x,&k);
ll ans=slove(x,k);
printf("Case #%d: %lld\n",t,ans);
}
return 0;
}