GCD and LCM
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 2261 Accepted Submission(s): 999
Problem Description
Given two positive integers G and L, could you tell me how many solutions of (x, y, z) there are, satisfying that gcd(x, y, z) = G and lcm(x, y, z) = L?
Note, gcd(x, y, z) means the greatest common divisor of x, y and z, while lcm(x, y, z) means the least common multiple of x, y and z.
Note 2, (1, 2, 3) and (1, 3, 2) are two different solutions.
Note, gcd(x, y, z) means the greatest common divisor of x, y and z, while lcm(x, y, z) means the least common multiple of x, y and z.
Note 2, (1, 2, 3) and (1, 3, 2) are two different solutions.
Input
First line comes an integer T (T <= 12), telling the number of test cases.
The next T lines, each contains two positive 32-bit signed integers, G and L.
It’s guaranteed that each answer will fit in a 32-bit signed integer.
The next T lines, each contains two positive 32-bit signed integers, G and L.
It’s guaranteed that each answer will fit in a 32-bit signed integer.
Output
For each test case, print one line with the number of solutions satisfying the conditions above.
Sample Input
2 6 72 7 33
Sample Output
72 0
思路:找规律,很明显这个和L/G有关,L无法整除G就是0个。
设d = L/G; d = 1 ans = 1 1 1 1
d = 2 ans = 6 1 1 2 x 3 1 2 2 x 3
d = 3 ans = 6 1 1 3 x 3 1 3 3 x 3
d = 4 ans = 12 1 1 4 x 3 1 4 4 x 3 1 2 4 x 6
d = 5 ans = 6 1 1 5 x 3 1 5 5 x 3
d = 6 ans = 36 1 1 6 x 3,1 2 6 x 6, 1 3 6 x 6, 1 6 6 x 3,1 2 3 x 6,2 2 3 x 6, 2 3 6 x 6 = 36
推到这基本就能推测出结论了。把d质数分解,一个质数就x6如果这个质数出现了多次,就再乘上这个质数出现的次数,然后这个结论在12(样例)上也能行得通。所以正解就是质数分解,再求乘积。
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
typedef long long ll;
vector<int> pi;
int vis[102400];
void init()
{
for(int i = 2;i <= 100000;i++)
{
if(!vis[i])
{
pi.push_back(i);
int t = i*2;
while(t <= 100000)
{
vis[t] = 1;
t+=i;
}
}
}
}
int main()
{
init();
int t;
scanf("%d",&t);
while(t--)
{
memset(vis,0,sizeof(vis));
ll a,b;
scanf("%lld%lld",&a,&b);
if(b%a)
{
printf("0\n");
continue;
}
ll tmp = b/a;
ll sum = 1;
for(int i = 0;i < pi.size();i++)
{
if(tmp%pi[i])continue;
int num = 0;
while(tmp%pi[i]==0)
{
num++;
tmp/=pi[i];
}
sum*=num*6;
}
if(tmp>1)
{
sum*=6;
}
printf("%I64d\n",sum);
}
return 0;
}