HDU5936 Difference 【中途相遇法】

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

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
1T100
0x109
1K9
 

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


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值