您的位置:首页 > 其它

HDU1.2.3 hide handkerchief

2015-08-03 20:43 351 查看
最开始以为只是一道超级水的模拟题,但是用以下的模拟会超内存限制

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
bool a[100000001];

int main()
{
int N,M;
scanf("%d%d",&N,&M);
while(N!=-1&&M!=-1){
int temp=1;
memset(a,0,sizeof(a));
while(a[temp]!=1){
a[temp]=1;
temp+=M;
temp=temp%(N+1)+1;
}
int flag=0;
for(int i=1;i<=N;i++)
if(a[i]==0){
flag=1;
break;
}
if(flag)
printf("POOR Haha\n");
else
printf("YES\n");
scanf("%d%d",&N,&M);
}
return 0;
}


但是也实在想不出什么方法,于是去百度了一发,得知其结论为M与N互质,但是证了好久还是证不来,看来自己数学功底真的不行!

既然知道了互质,那么就是欧几里得求最小公约数即可,代码如下:

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;

int gcd(int m,int n)
{
if(m<n){
int temp=n;
n=m;
m=temp;
}
if(n==0)
return m;
else
return gcd(n,m%n);

}

int main()
{
int N,M;
scanf("%d%d",&N,&M);
while(N!=-1&&M!=-1){
if(gcd(N,M)==1)
printf("YES\n");
else
printf("POOR Haha\n");
scanf("%d%d",&N,&M);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: