Little Ruins is playing a number game, first he chooses two positive integers yy and KK and calculates f(y,K)f(y,K), here

f(y,K)=∑z in every digits of yzK(f(233,2)=22+32+32=22)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)−yx=f(y,K)−y

As Ruins is forgetful, a few seconds later, he only remembers KK, xx and forgets yy. please help him find how many yy satisfy x=f(y,K)−yx=f(y,K)−y.

InputFirst line contains an integer TT, which indicates the number of test cases.

Every test case contains one line with two integers xx, KK.

Limits 
1≤T≤1001≤T≤100 
0≤x≤1090≤x≤109 
1≤K≤91≤K≤9OutputFor 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

题意:给定函数f(y,K)=∑y的所有位的K次幂,给出X,K,问多少个Y满足f(Y,K)=X;

思路:似乎没有什么思路,考虑暴力,估计Y只有10位,所以我们暴力前面5位,用map去寻找后面5位。

然而我开始些了离散化WA了,map过了。

#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
unordered_map<ll,int>mp;
ll tot,cnt,f[][],K,a[maxn],X,ans,num[maxn];
int main()
{
int T,Cas=;
scanf("%lld",&T); rep(i,,) f[i][]=;
rep(i,,) rep(j,,) f[i][j]=f[i][j-]*i;
while(T--){
tot=cnt=; mp.clear();
scanf("%lld%lld",&X,&K); ans=;
rep(i,,)
rep(j,,)
rep(k,,)
rep(p,,)
rep(q,,){
ll t1=f[i][K]+f[j][K]+f[k][K]+f[p][K]+f[q][K],t2=i*+j*+k*+p*+q;
a[++tot]=t1-100000LL*t2; mp[t1-t2]++;
if(t1-t2==X) ans++;
}
rep(i,,tot) if(mp.find(X-a[i])!=mp.end())ans+=mp[X-a[i]];
printf("Case #%d: %lld\n",++Cas,ans-(X==));
}
return ;
}

错误代码(依然没有找到bug):

#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
int Laxt[maxn],Next[maxn],tot,cnt,f[][],num[maxn]; ll a[maxn],To[maxn],X,ans,K;
int find(ll x){
ll t=(x%maxn+maxn)%maxn;
for(int i=Laxt[t];i;i=Next[i]) if(To[i]==x) return num[i];
return ;
}
void add(ll x){
if(x==X) ans++;
ll t=(x%maxn+maxn)%maxn;
for(int i=Laxt[t];i;i=Next[i]){
if(To[i]==x) { num[i]++; return ;}
}
Next[++cnt]=Laxt[t]; Laxt[t]=++cnt; To[cnt]=x; num[cnt]=;
}
int main()
{
int T,Cas=;
scanf("%lld",&T); rep(i,,) f[i][]=;
rep(i,,) rep(j,,) f[i][j]=f[i][j-]*i;
while(T--){
memset(Laxt,,sizeof(Laxt));
tot=cnt=;
scanf("%lld%lld",&X,&K); ans=;
rep(i,,)
rep(j,,)
rep(k,,)
rep(p,,)
rep(q,,){
ll t1=f[i][K]+f[j][K]+f[k][K]+f[p][K]+f[q][K],t2=i*+j*+k*+p*+q;
a[++tot]=t1-100000LL*t2; add(t1-t2);
}
rep(i,,tot) ans+=find(X-a[i]);
printf("Case #%d: %lld\n",++Cas,ans-(X==));
}
return ;
}

HDU - 5936: Difference(暴力:中途相遇法)的更多相关文章

  1. HDU 5936 Difference 【中途相遇法】(2016年中国大学生程序设计竞赛(杭州))

    Difference Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  2. 【UVALive】2965 Jurassic Remains(中途相遇法)

    题目 传送门:QWQ 分析 太喵了~~~~~ 还有中途相遇法这种东西的. 嗯 以后可以优化一些暴力 详情左转蓝书P58 (但可能我OI生涯中都遇不到正解是这个的题把...... 代码 #include ...

  3. LA 2965 中途相遇法

    题目链接:https://vjudge.net/problem/UVALive-2965 题意: 有很多字符串(24),选出一些字符串,要求这些字符串的字母都是偶数次: 分析: 暴力2^24也很大了, ...

  4. 紫书 习题 8-16 UVa 1618 (中途相遇法)

    暴力n的四次方, 然而可以用中途相遇法的思想, 分左边两个数和右边两个数来判断, 最后合起来判断. 一边是n平方logn, 合起来是n平方logn(枚举n平方, 二分logn) (1)两种比较方式是相 ...

  5. 【uva 1152】4 Values Whose Sum is Zero(算法效率--中途相遇法+Hash或STL库)

    题意:给定4个N元素几个A,B,C,D,要求分别从中选取一个元素a,b,c,d使得a+b+c+d=0.问有多少种选法.(N≤4000,D≤2^28) 解法:首先我们从最直接最暴力的方法开始思考:四重循 ...

  6. HDU 5936 Difference

    题意: 有一个函数f(y, k) = y的每个十进制位上的数字的k次幂之和 给x, k 求 有多少个y满足 x = f(y, k) - y 思路: (据说这叫中途相遇法?) 由于 x >= 0 ...

  7. uva 6757 Cup of Cowards(中途相遇法,貌似)

    uva 6757 Cup of CowardsCup of Cowards (CoC) is a role playing game that has 5 different characters (M ...

  8. LA 2965 Jurassic Remains (中途相遇法)

    Jurassic Remains Paleontologists in Siberia have recently found a number of fragments of Jurassic pe ...

  9. 【中途相遇法】【STL】BAPC2014 K Key to Knowledge (Codeforces GYM 100526)

    题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...

  10. 高效算法——J 中途相遇法,求和

    ---恢复内容开始--- J - 中途相遇法 Time Limit:9000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Su ...

随机推荐

  1. iOS XCode工程 警告处理

    今天 老板说,群~你的警告⚠️蛮多的...我拍了胸脯,下周项目总结时候一定会完美解决!!! 于是我得把项目中全部警告解决了,加油

  2. C语言预处理命令之条件编译(#ifdef,#else,#endif,#if等)

    转自:http://www.kuqin.com/language/20090806/66164.html 预处理过程扫描源代码,对其进行初步的转换,产生新的源代码提供给编译器.可见预处理过程先于编译器 ...

  3. C/C++中0xcccccccc...

    * 0xABABABAB : Used by Microsoft's HeapAlloc() to mark "no man's land" guard bytes after a ...

  4. 官方online ddl

    一.5.6版本online DDL 操作 In Place Rebuilds Table Concurrent DML Only Modifies Metadata 说明 add/create sec ...

  5. HTML5 SVG世界地图

    在线演示 本地下载

  6. redis安装优化:

    1)内存分配控制: vm.overcommit_memoryredis启动时肯呢个会出现这样的日志: :M Apr ::! Background save may fail under low mem ...

  7. IntelliJ Idea 常用功能及其快捷键总结(长期更新,纯手动)

    基础功能总结 快捷键总结 全局搜索 CTRL SHIF F 局部搜索 CTRL F 替换 CTRL R 复制一行 CTRL D 剪切一行 CTRL X 行定位 CTRL G 文件重命名 SHIFT F ...

  8. 尽可能的构建一个拓展性比"较好"的项目,会让你后期迭代好受点

    转载请注明出处:王亟亟的大牛之路 这礼拜基本都在忙自己项目上的事,然后之后会"重新整理"后把这部分的功能开源出来,这里@下队友 NeglectedByBoss 本周还是没有停更收纳 ...

  9. Nodejs WEB开发常用库和框架

    我在Nodejs的体系里也算泡了很久了,的确非常喜欢javascript和Nodejs. 在我看来,用nodejs做web开发有以下几个优点: Javascript作为一个语法异常简单的脚本语言,约束 ...

  10. JS Object To C# ASP.Net ModelBind

    之前做项目的时候发现,Jquery自带的Form 序列化函数.与asp.net 里边的Modelbinding格式不匹配,所以写了一个可以把前端的Object对象序列化成ModelBinding认识的 ...