大意:给定长度n,wolf从0开始捕捉兔子,下一点是k%n,再下一点是2k%n,3k%n……问,有没有点是wolf不能访问到的。
可以发现,只有wolf一次移动的距离和总的长度的最大公约数是1,所有的点才都能被遍历。
#include <iostream> #include <cstdio> #define LL long long using namespace std; LL gcd(LL a, LL b){ return b?gcd(b,a%b):a; } int main() { LL T,m,n; while(cin>>T){ while(T--){ LL f; scanf("%lld%lld",&m,&n); if(gcd(m,n)==1){ printf("NO\n"); } else printf("YES\n"); } } return 0; }
#include <iostream> #include <cstdio> using namespace std; typedef long long LL; const int maxn=1e6+5; int gcd(int a,int b){ return b?gcd(b,a%b):a; } int fac[maxn],top; int main(){ int m,n; while(cin>>m>>n){ top=0; for(int i=1;i<=m;i++){ //m可能等于1.所以要写<= if(gcd(m,i)==1) fac[++top]=i; } LL ans; if(n%top==0) ans=m*(n/top-1)+fac[top]; else ans=m*(n/top)+fac[n%top]; printf("%lld\n",ans); } return 0; }
Given x and y (2 <= x <= 100,000, 2 <= y <= 1,000,000), you are to count the number of p and q such that:
1) p and q are positive integers;
2) GCD(p, q) = x;
3) LCM(p, q) = y.
Input
x and y, one line for each test.
Output
Number of pairs of p and q.
Sample Input
3 60
Sample Output
4
分析:GCD(p, q) = x; --> GCD(p/x,q/x)=1;#include <iostream> #include <cstdio> #include <cstring> using namespace std; int fac[1010],top; void resolve(int a){ top=0; for(int i=2;i*i<=a;i++){ if(a%i==0){ fac[top++]=i; while(a%i==0) a/=i; } } if(a>1) fac[top++]=a; } int power(int p){ int ans=1,temp=2; while(p){ if(p&1) ans=ans*temp; temp=temp*temp; p>>=1; } return ans; } int main() { int x,y; while(cin>>x>>y){ if(y%x){ printf("0\n"); continue; } int w=y/x; resolve(w); printf("%d\n",power(top)); } return 0; }hdu 1019 Least Common Multiple
#include <iostream> #include <cstdio> using namespace std; int gcd(int a,int b){ return b?gcd(b,a%b):a; } int a[10000]; int main() { //freopen("cin.txt","r",stdin); int t,m; cin>>t; while(t--){ int g,ans=1; scanf("%d%d",&m,&a[0]); ans=a[0]; for(int i=1;i<m;i++){ scanf("%d",&a[i]); g=gcd(ans,a[i]); ans=ans/g*a[i]; } printf("%d\n",ans); } return 0; }