首页 > 代码库 > [记忆化搜索] zoj 3681 E - Cup 2
[记忆化搜索] zoj 3681 E - Cup 2
题目链接:
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3681
The European Cup final is coming. The past two World Cup winners, Spain and Italy, will contest the decider at Kiev‘s Olympic Stadium. Italy-Spain Euro final promises to be clash of polar opposites, so it‘s difficult to say which team will win.
Now there are M fans in ZJU, N of them support Italy. Suppose you are the president of the students‘ union and you support Italy. You can divide these M fans into s1 groups(Level 1). More than half of the group(Level 1) support Italy ,than you can say it seems all the M fans support Italy. You can also divide each group(Level 1) into s2 groups(Level 2). More than half of the group(Level 2) support Italy ,than you can say this group(Level 1) support Italy. ... .You can also divide each group(Level i) into s(i+1) groups(Level i+1). More than half of the group(Level i+1) support Italy ,than you can say this group(Level i) support Italy. To be fair, every group(Level i) has the same number of person. Can you find an suitable way to arrange these N person so that all these M fans seem to support Italy.
Input
Mutiple test cases, process to the end of file.
Each case has a single line with two integer M , N (1<=M,N<=100000000).
Output
For each case:
The firt line output Yes if you can do the task or No for not. The second line output the minimum person you need.
Sample Input
4 3 12 5
Sample Output
Yes 3 No 6
Author: HE, Yueyang
Contest: ZOJ Monthly, January 2013
题目意思:
给一个n,m,n表示n个人,可以把n个人分成k组,每组n/k个人,人数要一样,如果超过一半的组支持Italy的话,说明这n个人都支持Italy.可以把这k组中任意一组或多组再继续往下分,假设再把n/k分成p组,如果这p组中有超过一半的组支持Italy的话,说明n/k是支持Italy的,如此类推。求最少需要多少人支持Italy,才能确保这n个人都支持Italy.
解题思路:
记忆化搜索
用map保存已经求出结果的。每次求的时候枚举1~sqrt(cur)是否为约数,分成两个部分。
#include<iostream> #include<cstdio> #include<vector> #include<cstring> #include<map> #include<cmath> #define ll long long #include<cstdlib> #define N 100000 using namespace std; map<int,int>myp; int n,m; int dfs(int cur) { if(myp[cur]) return myp[cur]; if(cur==1) return 1; if(cur==2) return 2; int ans=cur/2+1; for(int i=2;i<=sqrt(1.0*cur);i++) { //printf("i:%d cur:%d\n",i,cur); //system("pause"); if(cur%i==0) { int a=dfs(i); int b=dfs(cur/i); //printf("cur:%d i:%d a:%d b:%d\n",cur,i,a,b); //system("pause"); ans=min(ans,a*(cur/i/2+1)); ans=min(ans,b*(i/2+1)); } } return myp[cur]=ans; } int main() { //printf("%d\n",dfs(100)); myp.clear(); while(~scanf("%d%d",&n,&m)) { int ans=dfs(n); if(ans<=m) printf("Yes\n"); else printf("No\n"); printf("%d\n",ans); } return 0; }