HDU 5936 Difference 折半枚举,暴力

/*
   观察函数显然到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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值