标题: [数学题]zju_1577 [打印本页] 作者: sheep426 时间: 2005-7-23 12:50 标题: [数学题]zju_1577 http://acm.zju.edu.cn/show_problem.php?pid=1577
GCD & LCM
Time limit: 1 Seconds Memory limit: 32768K
Total Submit: 5655 Accepted Submit: 1520
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 Author: ZHOU, Qiang SubmitBackStatus Zhejiang University Online Judge V1.0
[ Last edited by sheep426 on 2005-7-23 at 12:59 ]作者: sheep426 时间: 2005-7-23 12:53
我的代码,由于还是模拟,所以很慢,有很大的优化空间
Accepted 1577 C++ 00:00.67 920K sheep
[ Last edited by 剑乱 on 2005-7-23 at 13:06 ]作者: sheep426 时间: 2005-7-23 13:15
这样不好点吗?
#include<stdio.h>
int x,y;
int check(int a,int b)
{
int t,t2;
t2=a*b;
while(a%=b)
{
t=a;
a=b;
b=t;
}
t2/=b;
if(x==b&&y==t2)
return 1;
else
return 0;
}
int main()
{
int i,sum,product,t3;
while(scanf(\"%d %d\",&x,&y)!=EOF)
{
sum=0;
product=x*y;
for(i=x;i<product;i=i+x)
{
t3=product/i;
if(i>t3)
break;
if(!(t3%x))
{
if(check(i,t3))
sum++;
}