E - Cup 2 dfs记忆化搜索

Description

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 Mfans 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
***********************************************************************************************************************************************************
枚举所有的组,记忆化搜索
*********************************************************************************************************************************************************U*
 1 #include<iostream>
 2 #include<string>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<cstdio>
 6 #include<map>
 7 using namespace std;
 8 map<int,int>ans;
 9 int n,m;
10 int dfs(int n)
11 {
12   int MIN=n/2+1;
13   int it;
14   if(ans.count(n))
15     return ans[n];
16   for(it=2;it*it<=n;it++)
17   {
18     if(n%it==0)//枚举所有的分组
19     {
20         MIN=min(MIN,((n/it)/2+1)*dfs(it));
21         MIN=min(MIN,(it/2+1)*dfs(n/it));
22     }
23   }
24   ans[n]=MIN;//分成对应的组,所需要的最小值
25   return MIN;
26 }
27 int main()
28 {
29     while(scanf("%d %d",&n,&m)!=EOF)
30     {
31         int  MIN=dfs(n);
32         if(MIN<=m)
33             puts("Yes");
34         else
35             puts("No");
36         printf("%d\n",MIN);
37     }
38     return 0;
39 }
View Code

转载于:https://www.cnblogs.com/sdau--codeants/p/3444324.html

猜你喜欢

转载自blog.csdn.net/weixin_34318272/article/details/93432899
CUP