HDU 5936 Difference 折半枚举,暴力

来源:互联网 发布:运营商的云计算项目 编辑:程序博客网 时间:2024/06/07 07:11
/*   观察函数显然到1e10左右为负   先处理一半 再根据x 匹配另一半。*/#include<bits/stdc++.h>using namespace std;#define rep(i,a,b) for(int i=(a);i<=(b);++ i)typedef long long ll;const int N = 1e5+1;multiset<int> s[8];multiset<ll> s9;ll num[10][N];int Pow[10][10];ll f(ll x,int k){    ll ans=0;    while(x) ans +=Pow[x%10][k],x /=10;    return ans;}const int limit =1e5;int main(){   rep(i,1,9) rep(j,1,9) Pow[i][j] = j==1?i:Pow[i][j-1]*i;    for(int k=1;k<=9;k++)    {        for(ll i=0;i<limit;i++)        {            num[k][i] =f(i,k)-(ll)i*limit;            if(k<=8) s[k-1].insert((f(i,k))-i);            else s9.insert((f(i,k))-i);        }    }    int t,cas=0;    cin>>t;    while(t--)    {        int x,k;        cin>>x>>k;        int ans=0;        for(int i=0;i<limit;i++)        {            int c;            ll xx =x- num[k][i];            if((xx>1e9||xx<-1e9)&&k<=8) continue;            if(k<=8) c =s[k-1].count(xx);            else c =s9.count(xx);            ans +=c;        }        if(x==0) ans--;        cout<<"Case #"<<++cas<<": "<<ans<<endl;    }    return 0;}

原创粉丝点击