题意
定义x=y各个位置上的数字的k次和-y本身
先已知x和k,求存在几个这样的y
题解
考虑x的范围为1e9,因此y不超过1e10,因此考虑折半枚举前5位和后5位,统计答案。
由于T有100组,因此可以预处理出9个数字的前5位的值,这样用map不会TLE,若每次都重新算一遍的话,map会TLE,使用vector+二分进行优化
代码
/**
* author: TelmaZzzz
* create: 2019-10-10-16.38.58
**/
#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <ctime>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <unordered_map>
//#include <random>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
void _R(int &x) { scanf("%d", &x); }
void _R(ll &x) { scanf("%lld", &x); }
void _R(db &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
void _W(const int &x) { printf("%d", x); }
void _W(const ll &x) { printf("%lld", x); }
void _W(const db &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
#define rep(x,y,z) for(int x=y;x<=z;x++)
#define erp(x,y,z) for(int x=y;x>=z;x--)
#define PB push_back
#define MP make_pair
#define INF 1073741824
#define inf 1152921504606846976
#define pi 3.14159265358979323846
#define Fi first
#define Se second
//#pragma comment(linker,"/STACK:10240000,10240000")
//mt19937 rand_(time(0));
const int N=3e5+7,M=2e6;
const long long mod=1e9+7;
inline int read(){int ret=0;char ch=getchar();bool f=1;for(;!isdigit(ch);ch=getchar()) f^=!(ch^'-');for(;isdigit(ch);ch=getchar()) ret=(ret<<1)+(ret<<3)+ch-48;return f?ret:-ret;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){ll ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}//逆元
//int head[N],NEXT[M],ver[M],tot;void link(int u,int v){ver[++tot]=v;NEXT[tot]=head[u];head[u]=tot;}
void TelmaZzzz(){
#ifndef ONLINE_JUDGE
freopen("1.txt","r",stdin);
#endif
}
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/priority_queue.hpp>
using namespace __gnu_pbds;
int k;
ll Pow(ll x,ll y){
ll res=1;
while(y){
if(y%2==0){
x*=x;
y/=2;
}
else {
res*=x;
y--;
}
}
return res;
}
ll getf(int x){
ll res=0;
while(x){
res+=Pow(1LL*x%10,1LL*k);
x/=10;
}
return res;
}
vector<ll>que;
int main(){
TelmaZzzz();
//ios::sync_with_stdio(false);
int t;
R(t);
ll x;
int ti=0;
while(t--){
R(x,k);
//cout<<getf(123)<<endl;
vector<ll>que_;
swap(que,que_);
rep(i,0,1e5){
ll tmp=getf(i);
// cout<<tmp<<endl;
que.PB(tmp-1LL*i*100000);
}
sort(que.begin(),que.end());
int ans=0;
rep(i,0,99999){
ll tmp=getf(i)-i;
tmp=x-tmp;
//if(mp.find(x-tmp)==mp.end()) continue;
int it1=upper_bound(que.begin(),que.end(),tmp)-que.begin();
int it2=lower_bound(que.begin(),que.end(),tmp)-que.begin();
ans+=it1-it2;
//cout<<it1<<' '<<it2<<endl;
}
printf("Case #%d: ",++ti);
if(x==0) ans--;
W(ans);
}
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}