HDU 5936 Difference(折半搜索(中途相遇法))

http://acm.hdu.edu.cn/showproblem.php?pid=5936

题意:

定义了这样一种算法,现在给出x和k的值,问有多少个y是符合条件的。

思路:

y最多只有10位,再多x就是负的了。

这样的话可以将y分为前后两部分,我们先枚举后5位的情况,然后再枚举前5位的情况,通过二分查找找到匹配的项,这样就大大的降低了时间复杂度。

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<vector>
 6 #include<stack>
 7 #include<queue>
 8 #include<cmath>
 9 #include<map>
10 #include<set>
11 using namespace std;
12 typedef long long ll;
13 const int INF = 0x3f3f3f3f;
14 const int maxn=100000+5;
15
16 int p[15][15];
17 int sum[maxn];
18 int num[maxn];
19 int x, k,cnt;
20
21 void init()  //处于后5位的情况,i是枚举,sum[i]存储的是对应k下的值
22 {
23     cnt=0;
24     memset(sum,0,sizeof(sum));
25     for(int i=1;i<100000;i++)
26     {
27         int tmp=i;
28         while(tmp)
29         {
30             sum[i]+=p[tmp%10][k];
31             tmp/=10;
32         }
33         num[cnt++]=sum[i]-i;
34     }
35     sort(num,num+cnt);
36 }
37
38 void solve()  //枚举前5位的值,然后二分查找即可
39 {
40     ll ans=0;
41     for(int i=0;i<100000;i++)
42     {
43         ll tmp=sum[i]-(ll)i*100000;
44         int idx=lower_bound(num,num+cnt,x-tmp)-num;
45         while(num[idx]==x-tmp && idx<cnt)
46         {
47             ans++;
48             idx++;
49         }
50     }
51     printf("%lld\n",ans);
52 }
53
54 int main()
55 {
56     //freopen("in.txt","r",stdin);
57     for(int i=1;i<10;i++)   //预处理i^j的值
58     {
59         p[i][0]=1;
60         for(int j=1;j<10;j++)
61             p[i][j]=p[i][j-1]*i;
62     }
63     int T;
64     int kase=0;
65     scanf("%d",&T);
66     while(T--)
67     {
68         printf("Case #%d: ",++kase);
69         scanf("%d%d",&x,&k);
70         init();
71         solve();
72     }
73     return 0;
74 }
时间: 2024-08-23 23:13:16

HDU 5936 Difference(折半搜索(中途相遇法))的相关文章

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

Difference Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 62    Accepted Submission(s): 19 Problem Description Little Ruins is playing a number game, first he chooses two positive integers y an

Codeforces Round #297 (Div. 2) E题. Anya and Cubes (中途相遇法)

题目地址:Anya and Cubes 比赛的时候居然没想起中途相遇法...这题也是属于想起来就很简单系列. 中途相遇法也叫折半搜索.就是处理前一半,把结果储存起来,再处理后一半,然后匹配前一半存储的结果. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib

codeforces 525 E Anya and Cubes 中途相遇法

codeforces 525 E Anya and Cubes 中途相遇法 题意: 给出n个数a1,a2,...,an,要求从中选出一些数,可以把其中最多k个变成它自己的阶乘,然后选出的数求和,问最后和等于s的选法有多少种. 限制: 1 <= n <= 25; 0 <= k <= n; 1<= s <= 1e16; 1 <= ai <= 1e9 思路: 一般数据量20~30都会考虑中途相遇法,就是折半暴力. ps:用三进制暴力会比直接深搜多一个常数10,因为

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

uva 6757 Cup of CowardsCup of Cowards (CoC) is a role playing game that has 5 di?erent characters (Mage, Tank, Fighter,Assassin and Marksman). A team consists of 5 players (one from each kind) and the goal is to kill amonster with L life points. The

uva1152 - 4 Values whose Sum is 0(枚举,中途相遇法)

用中途相遇法的思想来解题.分别枚举两边,和直接暴力枚举四个数组比可以降低时间复杂度.可是我不会写...看了紫书作者刘汝佳老师的代码,真是太美了!简单明了,就像看吕钦下的棋一样.我就模仿的写了一下: #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<map> #include<set>

POJ 1840 Eqs Hash + 中途相遇法

把等式分成两拨算用中途相遇法就好了. 不过要注意的是这里不能用map,会超时,要自己手写hash,我重载了[]操作符之后用起来和map差不多,很随意 #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <set> #include <vector> #include <string> #include <qu

【中途相遇法】【STL】BAPC2014 K Key to Knowledge

题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11674&courseid=0 题目大意: N个学生M道题(1<=N<=12,1<=M<=30),每道题只有正误两种选项(0 1),每个学生的答题情况和正确题数已知,求标准答案可能有多少种. 如果标准答案只有一种则输出标准答案,否则输出解的个数. 题目思路: [

J 中途相遇法,求和

---恢复内容开始--- J - 中途相遇法 Time Limit:9000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description The SUM problem can be formulated as follows: given four lists A, B, C, D<tex2html_verbatim_mark> of integer values, compute

Codeforces 31E TV Game 中途相遇法 状压dp

题目链接:点击打开链接 题意: 给定2*n长的数字. 把这个数字拆成2个长度为n的数字,且相对位置不变.使得拆后得到的2个数字的和最大. 输出一个方案. 显然是中途相遇法,先计算左半段,再计算右半段 分别状压左半段和右半段,注意左半段状压后要在末尾补上0. 代码估计哪里有小越界==,数组开大了一点才过..具体就不查了. #include<iostream> #include<stdio.h> #include<string.h> #include<string&g

UVA 1326 Jurassic Remains 中途相遇法

题目链接:点击打开链接 题意:给定n个字符串,选尽可能多的字符串使得每种字母出现的次数为偶数次 思路: 中途相遇法 import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.LinkedLi