2016 ccpc 杭州 D.Difference hdu5936(折半枚举)

 

 有坑!!!当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;
}
二分

 

#include <bits/stdc++.h>
#define pi acos(-1);
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int INF = 0x3f3f3f3f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 150000 + 10;
const int mod = 1e9 + 7;

map<LL, LL> mp[15];

LL F(LL y, LL k)
{
    LL tmp, ans=0, mul=1;
    while(y){
        tmp = y%10;
        mul = 1;
        for(LL i=1; i<=k; i++) mul *= tmp;
        ans += mul;
        y/=10;
    }
    return ans;
}

void init()
{
    for(LL k=1; k<=9; k++){
        for(LL i=0; i<100000; i++){
            mp[k][F(i, k)-i*100000]++;
            //if(F(i, k)-i*100000 == 0) printf("----%lld %lld\n", i, k);
        }
    }
}

int main()
{
    init();
    LL T, k, x, cas=0; scanf("%lld", &T);
    while(T--){
        scanf("%lld%lld", &x, &k);
        LL ans = 0;
        for(LL i=1; i<=99999; i++){
            if(mp[k].count(x-F(i, k)+i)){
                ans += mp[k][x-F(i, k)+i];
                //printf("...%lld\n", i);
            }
            //if(mp[k][x-F(i,k)+i]) printf("...%d %d %lld %lld\n", i, k, x-F(i, k)+i, mp[k][x-F(i,k)+i]);
        }
        printf("Case #%lld: %lld\n", ++cas, ans);
    }
}


/*
9
0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8
0 9

*/
STL

 

posted @ 2018-11-19 14:56  shuai_hui  阅读(233)  评论(0编辑  收藏  举报