[记忆化搜索] 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;
}

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

时间: 2024-11-07 21:00:32

[记忆化搜索] zoj 3681 E - Cup 2的相关文章

2014 Super Training #9 C E - Cup 2 --记忆化搜索

原题:ZOJ 3681 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3681 题意:给一个m,n,m表示m个人,可以把m个人分成k组,每组m/k个人,人数要一样,如果超过一半的组支持Italy的话,说明这n个人都支持Italy.同时又可以把这k组中任意一组或多组再继续往下分,假设再把m/k分成p组,如果这p组中有超过一半的组支持Italy的话,说明m/k是支持Italy的,如此类推.给定有n个人支持Italy,问能否使

ZOJ 3644 Kitty&#39;s Game dfs,记忆化搜索,map映射 难度:2

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4834 从点1出发,假设现在在i,点数为sta,则下一步的点数必然不能是sta的因数,所以不会形成环,只需从1直接走,走到n即可. 但是如果这样的话时空复杂度就都是nk,明显不满足题意,而这个时候我们可以想到,每个状态都必然是k的约数,(点数不是k的约数的节点不在路上,可以无视),而约数的个数也就k^0.5个,可以直接用map映射,这样时空复杂度都是n*k^0.5,可以解出答案

Zoj 1671 Walking Ant(BFS+优先队列||记忆化搜索)

Walking Ant Time Limit: 2 Seconds Memory Limit: 65536 KB 点击打开链接 Ants are quite diligent. They sometimes build their nests beneath flagstones. Here, an ant is walking in a rectangular area tiled with square flagstones, seeking the only hole leading to

HDU 1513 Palindrome:LCS(最长公共子序列)or 记忆化搜索

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 题意: 给你一个字符串s,你可以在s中的任意位置添加任意字符,问你将s变成一个回文串最少需要添加字符的个数. 题解1(LCS): 很神奇的做法. 先求s和s的反串的LCS,也就是原串中已经满足回文性质的字符个数. 然后要变成回文串的话,只需要为剩下的每个落单的字符,相应地插入一个和它相同的字符即可. 所以答案是:s.size()-LCS(s,rev(s)) 另外,求LCS时只会用到lcs[i-

uva 1076 - Password Suspects(AC自动机+记忆化搜索)

题目链接:uva 1076 - Password Suspects 题目大意:有一个长度为n的密码,存在m个子串,问说有多少种字符串满足,如果满足个数不大于42,按照字典序输出. 解题思路:根据子串构建AC自动机,然后记忆化搜索,dp[i][u][s]表示第i个字符,在u节点,匹配s个子串. #include <cstdio> #include <cstring> #include <queue> #include <string> #include <

poj 1579(动态规划初探之记忆化搜索)

Function Run Fun Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17843   Accepted: 9112 Description We all love recursion! Don't we? Consider a three-parameter recursive function w(a, b, c): if a <= 0 or b <= 0 or c <= 0, then w(a, b

记忆化搜索,FatMouse and Cheese

1.从gird[0][0]出发,每次的方向搜索一下,每次步数搜索一下 for(i=0; i<4; i++) { for(j=1; j<=k; j++) { int tx=x+d[i][0]*j; int ty=y+d[i][1]*j; if(tx>=0&&tx<n&&ty>=0&&ty<n&&grid[x][y]<grid[tx][ty]) { int temp=memSearch(tx,ty); i

LightOJ1417 Forwarding Emails(强连通分量+缩点+记忆化搜索)

题目大概是,每个人收到信息后会把信息发给他认识的一个人如此下去,问一开始要把信息发送给谁这样看到信息的人数最多. 首先找出图中的SCC并记录每个SCC里面的点数,如果传到一个SCC,那么里面的人都可以看到信息. 然后SCC缩点后就形成DAG,直接记忆化搜索,d(u)搜索从u点出发开始传最多能传多少人. 最后就是找答案了. 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namesp

POJ1088(记忆化搜索)

经典记忆化搜索题目.当 从每个点一次进行搜索时要采用 记忆化搜索 #include"cstdio" #include"algorithm" using namespace std; const int MAXN=105; int g[MAXN][MAXN]; int t[MAXN][MAXN]; int maxn; int n,m; int by,bx; int ans; int dx[4]={1,0,-1,0}; int dy[4]={0,1,0,-1}; int