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 ,
Input
First line contains an integer
T
, which indicates the number of test cases.
Every test case contains one line with two integersx ,
K
.
Limits
1≤T≤100
0≤x≤109
1≤K≤9
Every test case contains one line with two integers
Limits
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
题目大意:
已知一个函数
f(y,k)−y=x
,其中
f(y,k)
表示
y
的每一位的
解题思路:
因为有一个很重要的条件就是
trick:
用 map 记录超时了,所以直接
upper_bound−lower_bound
就OK了
代码:
#include <iostream>
#include <string.h>
#include <string>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <map>
using namespace std;
typedef long long LL;
const int MAXN = 1e5+5;
const double PI = acos(-1);
const double eps = 1e-8;
const LL MOD = 1e9+7;
LL f[10][10], g[MAXN][10];
LL cal(LL y, int k){
LL ans = 0;
while(y){
ans += f[y%10][k];
y /= 10;
}
return ans;
}
void Init1(){
for(int i=1; i<=9; i++){
for(int j=0; j<10; j++){
f[j][i] = j;
for(int k=2; k<=i; k++) f[j][i] *= j;
}
}
}
void Init(){
Init1();
for(int i=1; i<=9; i++) for(int j=1; j<=100000; j++) g[j][i] = cal(j, i);
}
LL a[MAXN];
int main(){
//freopen("C:/Users/yaonie/Desktop/in.txt", "r", stdin);
//freopen("C:/Users/yaonie/Desktop/out.txt", "w", stdout);
Init();
int T; scanf("%d",&T);
for(int cas=1; cas<=T; cas++){
LL x; int k; scanf("%lld%d",&x,&k);
LL ans = 0;
for(int i=1; i<=100000; i++) a[i] = g[i][k]-i;
sort(a+1, a+100001);
for(LL i=0; i<=100000; i++){
LL tmp = g[i][k]-i*100000;
tmp = x-tmp;
ans += upper_bound(a+1, a+100001, tmp)-lower_bound(a+1, a+100001, tmp);
}
printf("Case #%d: %lld\n",cas,ans);
}
return 0;
}