HDU 4497GCD and LCM(素數分解)
GCD and LCM
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 188 Accepted Submission(s): 94
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
Source
題目大意:找出三個數,他們的gcd是g他們的lcm是l。問你有多少組這樣的數字。
解題思路:如果想到把兩個數分解成素數,會很簡單。g分解成a1^n1*a2^n2*a3^n3...l也分解成b1^m1*b2^m2...
會發現l的因子必須把g的因子含進去,不光是類別而且是每個類的數目。我們可以直接判別l%g!=0的話就直接輸出0. l%g==0的話l=l/g。然後只需要對l分解成素數即可。l=b1^m1*b2^m2...假設三個數滿足題目的條件則有lcm(q1,q2,q3)=l
gcd(q1,q2,q3)=1,設這三個數為q1,q2,q3,q1=b1^s1*q2^s4...,q2=b1^s2*q2^s5...,q3=b1^s3*q2^s6.
由於lcm(q1,q2,q3)=l gcd(q1,q2,q3)=1
所以min(s1,s2,s3)=0,max(s1,s2,s3)=m1.s1取0,s2取m1,s3取1~m1,這樣的共有(m1-1)*6,6是排列數。s1,s2取0,s3取m1的共有3。 s1,s2取m1,s3取0的也有3種。 以上三種情況裡的s1,s2,s3順序可以排列,所以已經乘上了組合數。三者相加便是6*m1。然後依次其它計算方式一樣然後答案就是6*m1*6*m2*6*m3.....詳見程式碼。(開始準備素數打表,T總共才12無需打標)
題目地址:GCD and LCM
AC程式碼:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
int isprim(int x) //判定是素數
{
int flag=1,i;
for(i=2;i<=sqrt(x+0.5);i++)
if(x%i==0)
{
flag=0;
break;
}
return flag;
}
int main()
{
int tes,g,l,i;
scanf("%d",&tes);
while(tes--)
{
scanf("%d%d",&g,&l);
if(l%g!=0) puts("0");
else
{
l=l/g;
__int64 sum;
sum=1;
for(i=2;i<=sqrt(l+0.5);i++)
if(isprim(i)&&(l%i==0))
{
int p=0;
while(l%i==0)
{
l/=i;
p++;
}
sum=sum*6*p; //剛推的式子
}
if(l>1) sum*=6; //比如說9999997這樣的數
printf("%I64d\n",sum);
}
}
return 0;
}
相關文章
- HDU44979 GCD and LCM (素因子分解+計數)GC
- HDU 4497 GCD and LCM(拆素數+組合)GC
- HDU 5317 RGCDQ (素因子分解+預處理)GC
- Find Terrorists(素數篩選+素因子分解)Error
- UVA 10892 LCM Cardinality (分解因數+暴力)
- HDU 4135 Co-prime(容斥原理+分解質因數)
- 分數的GCD和LCMGC
- HDU 1299 Diophantus of Alexandria (公式變形 分解質因數)公式
- CodeForces - 463E Caisa and Tree (dfs+素因子分解)AI
- 大質數分解模板
- ACM 素數ACM
- HDU 4542 小明系列故事——未知剩餘系 (DFS 反素數 篩子預處理)
- 階乘質因數分解
- HDU 4548美素數(簡單題 儲存結果時需要注意不要超時)
- 素數計數函式函式
- 【數論】素數篩法
- LCM介面設計
- HDU 1452 Happy 2004(唯一分解定理)APP
- CF 1977 C. Nikita and LCM (*1900) 數論
- 《整數分解》讀書筆記筆記
- 質數判斷、質因子分解、質數篩
- 計算素數【Java】Java
- LCM模組生產流程
- 使用 LCM 加速生圖
- 求素數(質數)演算法演算法
- 表示素數的函式函式
- 密碼工程-大素數密碼
- 如何高效尋找素數
- [atcoder 349] [F - Subsequence LCM]
- HDU 2662 Coin && HDU 1792 A New Change Problem (互質數最大不能生成數)
- 初等數論——素數,逆元,EXGCD有關GC
- 素數個數 <埃式篩 && 尤拉篩>
- 素數迴文——輸出兩整數之間所有既是迴文數又是素數的數 C++實現C++
- hdu 2097 Java Sky數Java
- hdu 1792 A New Change Problem(數論)
- [演算法]: 素數篩法演算法
- MT6797 LCM設計指南
- 【快速因數分解】Pollard's Rho 演算法演算法