1. 程式人生 > >bzoj 2632 [neerc2011]Gcd guessing game——貪心(存疑)

bzoj 2632 [neerc2011]Gcd guessing game——貪心(存疑)

題目:https://www.lydsy.com/JudgeOnline/problem.php?id=2632

官方題解:http://neerc.ifmo.ru/archive/2011/neerc-2011-analysis.pdf

如果答案是1,就需要猜質數次;把質數分組,一組一組猜就行了。一組就是最大的一個和最小的幾個匹配。

然而既不知道為什麼這樣是最壞情況,也不知道為什麼最大的一個和最小的幾個匹配一定最優。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e4+5; int n,pri[N],cnt,ans;bool vis[N]; int main() { scanf("%d",&n); for(int i=2;i<=n;i++) { if(!vis[i])pri[++cnt]=i; for(int j=1;j<=cnt&&i*pri[j]<=n;j++) { vis[i*pri[j]]=1; if(i%pri[j]==0)break; } } int p0=1; for(int i=cnt;i;i--) {
if(p0>i)break; ans++;int ml=pri[i]; while(ml*pri[p0]<=n&&p0<i)ml*=pri[p0++]; } printf("%d\n",ans); return 0; }