码迷,mamicode.com
首页 > 其他好文 > 详细

[记忆化搜索] zoj 3681 E - Cup 2

时间:2014-05-15 14:56:27      阅读:393      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   c   tar   

题目链接:

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3681

E - Cup 2

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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
Submit    Status

题目意思:

给一个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;
}



[记忆化搜索] zoj 3681 E - Cup 2,布布扣,bubuko.com

[记忆化搜索] zoj 3681 E - Cup 2

标签:style   blog   class   code   c   tar   

原文地址:http://blog.csdn.net/cc_again/article/details/25727115

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!