UVA 题目1521 GCD Guessing Game(数论+贪心)
2015-08-14 12:17
465 查看
题意:一个数字x在1-n之间,现在猜数字,每次猜一个数字a,告知gcd(x, a)的答案,问最坏情况下需要猜几次
思路:在素数上考虑,猜一组素数的乘积的数字,就可以把这些素数组成的数字都猜出来,答案就是组数,这样问题就是如何分组使得组数最小,每次取最后一个,尽量和前面小的合并,就能使得组数最小
粘不了原题。。
ac代码
思路:在素数上考虑,猜一组素数的乘积的数字,就可以把这些素数组成的数字都猜出来,答案就是组数,这样问题就是如何分组使得组数最小,每次取最后一个,尽量和前面小的合并,就能使得组数最小
粘不了原题。。
ac代码
#include<stdio.h> #include<string.h> int head,tail; int prime[10005],vis[10005]; int cnt; void fun() { int i,j; cnt=0; for(i=2;i<10005;i++) { if(vis[i]) continue; prime[cnt++]=i; for(j=i+i;j<10005;j+=i) { vis[j]=1; } } } int main() { int n; fun(); while(scanf("%d",&n)!=EOF) { int head,tail,ans=0; head=0; tail=cnt-1; while(prime[tail]>n)tail--; while(head<=tail) { int p=prime[tail]; while(p*prime[head]<=n) { p=p*prime[head]; head++; } ans++; tail--; } printf("%d\n",ans); } }
相关文章推荐
- poj 2299 Ultra-QuickSort(树状数组)
- xcode工程文件格式(Xcode Project File Format)详解
- php require_once 的使用及与require的区别
- request对象的几个常用方法
- Win7安vc2008编译报LINK : fatal error LNK1000: Internal error during IncrBuildImage
- No enclosing instance of type WallpaperService is available due to some intermediate constructor inv
- 封装同步的UIActionSheet
- poj 3080 Blue Jeans
- Android.mk prebuild
- LeetCode:Unique Binary Search Trees Ⅱ
- hdu 1711 Number Sequence(kmp找子串第一次出现的位置)
- 使用4.3.11版本的hibernate结合druid连接池遇到的问题
- POJ 2533 Longest Ordered Subsequence
- easyui 密码框验证
- The Dole Queue
- UUID
- dlopen failed: empty/missing DT_HASH in "libx.so" (built with --hash-style=gnu?)
- hdu 1047 Integer Inquiry(大数)
- 物理机上使用fuel5.1部署openstackHA存储使用ceph
- 代码动态创建UITableViewCell小结