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

题目链接:

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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值