1. 程式人生 > >Pairs Forming LCM 【唯一分解定理】

Pairs Forming LCM 【唯一分解定理】

題意:問你滿足lcm(i,j)=n(i<=j)(i,j)有多少對。

思路:這東西肯定要先搞下質因子。由於n<=1014,那麼我們預處理質因子到107OK了,最後>=107的質因子最多隻會有一個。我們將n分解為若干個質因子a[i](1<=i<=k)
由唯一分解定理——n=ni=1(a[i]cnt[i]))(cnt[i]a[i])
我們想得到lcm(i,j)=n就必須滿足ij/gcd(i,j)的質因子與n的質因子完全相同。那我們考慮單個質因子a[i]:假設i=a[i]xj=a[i]y
這樣ij/gcd(i,j)=a[i]x+ymin(x

,y),則有x+ymin(x,y)=cnt[i]
一、x=cnt[i],顯然cnt[i]+yy=cnt[i],y值任意0<=y<=cnt[i]
二、0<=x<cnt[i],顯然要滿足等式,y=cnt[i]
這樣對於一個因子a[i],方案數為(cnt[i]+1+cnt[i]),但是(i,j)(j,i)肯定是重複計算,結果要除2。又去掉了(n,n)的情況,加一即可。

AC程式碼:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath> #include <algorithm> #include <vector> #include <queue> #include <map> #include <stack> #define PI acos(-1.0) #define CLR(a, b) memset(a, (b), sizeof(a)) #define fi first #define se second #define ll o<<1 #define rr o<<1|1 using namespace
std; typedef long long LL; typedef pair<int, int> pii; const int MAXN = 1e7 + 1; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; void getmax(int &a, int b) {a = max(a, b); } void getmin(int &a, int b) {a = min(a, b); } void add(LL &x, LL y) { x += y; x %= MOD; } bool vis[MAXN]; const int N = 1e6; int prime[N], top; void getprime() { top = 0; for(int i = 2; i < MAXN; i++) { if(vis[i]) continue; prime[top++] = i; for(int j = 2; j * i < MAXN; j++) vis[j*i] = true; } } int k, cnt[N]; void getp(LL n) { LL m = n; k = 0; for(int i = 0; i < top; i++) { if(m % prime[i] == 0) { int num = 0; while(m % prime[i] == 0) { num++; m /= prime[i]; } cnt[k++] = num; } if(prime[i] > n) break; } if(m > 1) { cnt[k++] = 1; } } int main() { getprime(); //cout << top << endl; int t, kcase = 1; scanf("%d", &t); while(t--) { LL N; scanf("%lld", &N); getp(N); LL ans = 1LL; for(int i = 0; i < k; i++) { ans = ans * (cnt[i] * 2 + 1); } ans >>= 1; printf("Case %d: %lld\n", kcase++, ans + 1); } return 0; }

相關推薦

Pairs Forming LCM 唯一分解定理

題意:問你滿足lcm(i,j)=n(i<=j)的(i,j)有多少對。 思路:這東西肯定要先搞下質因子。由於n<=1014,那麼我們預處理質因子到107就OK了,最後>=107的質

LightOJ 1236 Pairs Forming LCM唯一分解定理

題目分析 思路:把n分解成素因數的形式n=p1^c1+p2^c2+…pm^cm 假設已找到一對(a,b)的lcm=n 有a=p1^d1+p2^d2+…pm^dm b=p1^e1

數論Minimum Sum LCM, UVa10791唯一分解定理素數篩法

唯一分解定理+素數篩法 #include<bits/stdc++.h> using namespace std; typedef long long LL; int cnt,n,prime

數論Choose and Divide, UVa10375 組合數學唯一分解定理精度

唯一分解定理 #include<bits/stdc++.h> using namespace std; int p,q,r,s,prime[10005],cnt,e[10005];boo

LightOJ 1236 Pairs Forming LCM(算數基本定理

i+1 clas while 包含 兩個 min family clu eof 題意:在a,b中(a,b<=n)(1 ≤ n ≤ 1014),有多少組(a,b) (a<b)滿足lcm(a,b)==n; 先來看個知識點: 素因子分解:n = p1 ^ e

LightOJ-1236- Pairs Forming LCM (算術基本定理

原題連結: 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

LightOJ 1236 Pairs Forming LCM(算術基本定理

LightOJ 1236 Pairs Forming LCM 題意: long long pairsFormLCM( int n ) { long long res = 0;

題解LightOJ1236 Pairs Forming LCM 唯一分解定理+線性篩

題目連結 Input Input starts with an integer T (≤ 200), denoting the number of test cases. Each case starts with a line containin

H - Pairs Forming LCM(唯一分解定理)

align urn RM ctu rim tin div ans mes Find the result of the following code: long long pairsFormLCM( int n ) { long long res = 0; f

Pairs Forming LCM (素數,唯一分解定理

題目來源:https://vjudge.net/problem/LightOJ-1236 【題意】 求a,b的最小公倍數是n的個數。 【思路】 想到了素數,想到了唯一分解定理,但是唯獨沒有想到最

數論Sumdiv(整數的唯一分解定理+約束和公式+遞歸求等比)

ali 同余模公式 left 一個 c++ 出現 素數分解 code 特殊 來源:https://blog.csdn.net/lyy289065406/article/details/6648539 題目描述 Consider two natural numbers A a

ZCMU1796wjw的數學題(唯一分解定理+排列組合)

題目連結 1796: wjw的數學題 Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 70  Solved: 25 [Submit][Status][Web Board] Description

培訓題唯一分解定理應用[1] P1945

Description 質因子分解是數論中一個基本定理,見P1128。現在請你利用這個基本定理,對給定整數n,完成下列三個任務: 任務1、計算n的因數個數並由小到大輸出這些因數; 任務2、計算n的因數和; 任務3、計算1,2,…,n中與n互素的數個數:phi(n); Input 若干組資

uva 10375 唯一分解定理 篩法求素數數論

唯一分解理論的基本內容: 任意一個大於1的正整數都能表示成若干個質數的乘積,且表示的方法是唯一的。換句話說,一個數能被唯一地分解成質因數的乘積。因此這個定理又叫做唯一分解定理。 舉個栗子:50=(2^1)*(5^2)  題目一般的思路就是要把素數表打出來,eg上面的例子 e

定理算術基本定理唯一分解定理

大蒟蒻來水貼了! 算術基本定理(唯一分解定理) 一句話:      任何大於1的自然數,都可以唯一分解成有限個質數的乘積 例如對於大於1的自然數n, 這裡Pi均為質數,其指數ai是正

ACM整數唯一分解定理

n是大於一的任意正整數。(稱為標準分解式)其中pi為素數,質數ai為正整數(如果ai=0,相當於乘一,沒有意義的)。 標準分解式是唯一且一定存在的。(素因子的乘積順序不考慮) 下面給出幾個簡單的

HDU5579 LCM & 唯一分解定理 & 排列組合

Description Given two positive integers G and L, could you tell me how many solutions of (x, y, z

LightOJ 1341 Aladdin and the Flying Carpet(唯一分解定理

void 都是 scanf esp for space tar sqrt lld http://lightoj.com/volume_showproblem.php?problem=1341 題意:給你矩形的面積(矩形的邊長都是正整數),讓你求最小的邊大於等於b的矩形的個

HDU 1452 Happy 2004(唯一分解定理)

ring esp 鏈接 href names 全部 namespace 傳送門 http 題目鏈接:傳送門 題意: 求2004^x的全部約數的和。 分析: 由唯一分解定理可知 x=p1^a1*p2^a2*...*pn^an 那麽其約數和 sum = (p1^0+p1^1

hdu 1215 求約數和 唯一分解定理的基本運用

span pac pid vector type == 素數 ring 題意 http://acm.hdu.edu.cn/showproblem.php?pid=1215 題意:求解小於n的所有因子和 利用數論的唯一分解定理。 若n = p1^e1 * p2^e2 * ……