数论-约数

一.试除法求约数:

数论-约数

 1 vector<int> get_divisorts(int t){
 2     vector<int> a;
 3     for(int i = 1;i <= t/i;++i){
 4         if(t % i == 0){
 5             a.push_back(i);
 6             if(i != t/i) a.push_back(t/i);
 7         }
 8     }
 9     return a;
10 }

View Code

 

二.约数个数:

公式:由唯一分解定理: X = P1^a + P2^b + ... + Pn^m,约数个数 等于 (a+1) * (b+1) * ... * (m+1)

数论-约数

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 
 5 const int mod = 1e9+7;
 6 
 7 int main(){
 8     unordered_map<int, int> primes;
 9     ll res = 1;
10     int n;cin >> n;
11     while(n --){
12         int x; cin >> x;
13         //求出唯一分解定理里的每一项
14         for(int i = 2;i <= x/i;++i){
15             while(x % i == 0){
16                 x /= i;
17                 ++ primes[i];    
18             }
19         }
20         if(x > 1) ++ primes[x];
21     }
22     for(auto p : primes) res = res * (p.second+1) % mod;//约数个数等于唯一分解定理里每个指数+1再相乘  
23     cout << res << endl;
24     return 0;
25 }

View Code

 

三.约数之和:

公式:X = P1^a + P2^b + ... + Pn^m,约数之和 = (p1^0 + p1^1 + ... + p1^a) * (p2^0 + p2^1 + ... p2^b) * ... * (pn^0 + ... + pn^m)

数论-约数

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 typedef long long ll;
 5 const int mod = 1e9+7;
 6 
 7 int main(){
 8     unordered_map<int, int> prime;
 9     int n;cin >> n;
10     while(n --)
11     {
12         int x;cin>> x;
13         for(int i = 2;i <= x/i;++i){
14             while(x % i == 0){
15                 x /= i;
16                 prime[i]++;
17             }
18         }
19         if(x > 1)prime[x]++;
20     }
21     ll res = 1;
22     for(auto p : prime){
23         int a = p.first, b = p.second;        
24         ll t = 1;
25         while(b--) t = (t * a + 1) % mod;
26         res =res * t % mod;
27     }
28     cout << res << endl;
29     return 0;
30 }

View Code

 

四.gcd(欧几里得算法)(辗转相除法):求最大公约数:

公式:(a, b) = (b, a % b)

数论-约数

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 int gcd(int a, int b){
 5     return b ? gcd(b, a % b) : a;
 6 }
 7 
 8 int main(){
 9     int n;cin >> n;
10     while(n --){
11         int a, b;cin >> a >> b;
12         cout << gcd(a,b) << endl;
13     }
14     return 0;
15 }

View Code

 

原文链接: https://www.cnblogs.com/sxq-blog/p/12249887.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    数论-约数

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/326437

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月1日 下午3:48
下一篇 2023年3月1日 下午3:49

相关推荐