国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Pairs Forming LCM [數學][最小公倍數為n的數對]

2019-11-08 03:11:45
字體:
來源:轉載
供稿:網友

Find the result of the following code:

long long pairsFormLCM( int n ) { long long res = 0; for( int i = 1; i <= n; i++ ) for( int j = i; j <= n; j++ ) if( lcm(i, j) == n ) res++; // lcm means least common multiple return res;}

A straight forward implementation of the code may time out. If you analyze the code, you will find that the code actually counts the number of pairs (i, j) for which lcm(i, j) = n and (i ≤ j).

Input

Input starts with an integer T (≤ 200), denoting the number of test cases.

Each case starts with a line containing an integer n (1 ≤ n ≤ 1014).

Output

For each case, PRint the case number and the value returned by the function ‘pairsFormLCM(n)’.

Sample Input

15 2 3 4 6 8 10 12 15 18 20 21 24 25 27 29

Sample Output

Case 1: 2 Case 2: 2 Case 3: 3 Case 4: 5 Case 5: 4 Case 6: 5 Case 7: 8 Case 8: 5 Case 9: 8 Case 10: 8 Case 11: 5 Case 12: 11 Case 13: 3 Case 14: 4 Case 15: 2

解題報告

素因子分解一定要打素數表,不然超時 分析過程: a=pG11pG22…pGkkb=pH11pH22…pHkkn=pR11pR22…pRkk 若 LCM(a,b)=n ,即 ?????????R1=Max{G1,H1}R2=Max{G2,H2}…Rk=Max{Gk,Hk} 則a,b的組合個數 cnt=(C12?(R1+1)?1)?(C12?(R2+1)?1)?…?(C12?(Rk+1)?1) 由于a < b , 但a=b時只有一個,那么答案就是 ans=cnt/2+1

#include<stdio.h>#define MAX_N 10000100#define PN 664581#define I p[i]typedef long long LL;bool vis[MAX_N];int p[PN];void init(){ for(int i=2;i*i<MAX_N;i++) if(!vis[i]) for(int k=i*i;k<MAX_N;k+=i) vis[k]=true; for(int i=2,u=0;i<MAX_N;i++) if(!vis[i]) p[u++]=i;}LL solve(LL n){ LL res=1; for(LL i=0;(LL)I*I<=n;i++) if(n%I==0){ LL cnt=0; while(n%I==0) cnt++,n/=I; res*=2*cnt+1; } if(n>1) res*=3; return res/2+1;}int main(){ init(); int T; scanf("%d",&T); for(int t=1;t<=T;t++){ LL n; scanf("%lld",&n); printf("Case %d: %lld/n",t,solve(n)); } return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 北海市| 宁津县| 濉溪县| 禹城市| 苍山县| 新宾| 临沧市| 襄城县| 昂仁县| 海城市| 鄢陵县| 沈丘县| 广州市| 岑巩县| 文山县| 蒙阴县| 勃利县| 大洼县| 武清区| 五华县| 平泉县| 章丘市| 普陀区| 林周县| 南丰县| 灵寿县| 苏州市| 山阳县| 奇台县| 额尔古纳市| 赞皇县| 哈巴河县| 阳高县| 抚宁县| 德兴市| 五指山市| 遵化市| 台北县| 大田县| 枣阳市| 邻水|