Original question: zoj 3681 http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemcode = 3681.
Question: Give m, n, m to m people. You can divide m people into k groups. M/k people in each group should have the same number of people, if more than half of the groups support Italy, all N of them support Italy. at the same time, we can further divide any or multiple groups in the K group, assuming that M/K is further divided into P groups, if more than half of P groups support Italy, M/K supports Italy, and so on. Given that N people support Italy, ask if it seems that all m people support Italy. And the minimum number of people required to support Italy is required to ensure that all m people support Italy.
Practice: DFS appears that m people support the minimum number of Italy people, and then compare it with N. If res <= N, the number can be reached. Otherwise, the number cannot be reached.
When DFS is used, enumerate its approximate number (because it should be divided into groups with the same number of people), and then divide it to DFS.
Code:
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <map>using namespace std;#define N 1007std::map<int, int> ans;int DFS(int m){ if(ans.count(m)) return ans[m]; int mini = m/2+1; for(int i=2;i*i<=m;i++) { if(m%i == 0) { mini = min(mini,((m/i)/2+1)*DFS(i)); mini = min(mini,(i/2+1)*DFS(m/i)); } } ans[m] = mini; return ans[m];}int main(){ int n,m; ans.clear(); while(scanf("%d%d",&m,&n)!=EOF) { int res = DFS(m); if(res <= n) puts("Yes"); else puts("No"); printf("%d\n",res); } return 0;}
View code