zoukankan      html  css  js  c++  java
  • 约数

    1.试除法求约数

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <stack>
     7 #include <queue>
     8 #include <vector>
     9 #include <map>
    10 #include <set>
    11 #include <unordered_set>
    12 #include <unordered_map>
    13 #define ll long long
    14 #define fi first
    15 #define se second
    16 #define pb push_back
    17 #define me memset
    18 const int N = 1e6 + 10;
    19 const int M = 1e9 + 7;
    20 using namespace std;
    21 typedef pair<int,int> PII;
    22 typedef pair<long,long> PLL;
    23 
    24 int n;
    25 int x;
    26 vector<int> res;
    27 
    28 void divide(int n){
    29     res.clear();
    30     for(int i=1;i<=n/i;++i){
    31         if(n%i==0) {
    32             res.pb(i);
    33             if (i!=n/i) res.pb(n/i);
    34         }
    35     }
    36     sort(res.begin(),res.end());
    37     vector<int>::iterator iter;
    38      for(iter=res.begin();iter!=res.end();++iter) printf("%d ",*iter);
    39      printf("\n");
    40 }
    41 
    42 int main() {
    43     ios::sync_with_stdio(false);
    44     cin>>n;
    45      while(n--){
    46          cin>>x;
    47           divide(x);
    48      }
    49 
    50     return 0;
    51 }

    2.求约数个数/求约数之和

    分析:我们将n作质因数分解,利用约数个数定理和约数和定理即可

    求约数个数:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <stack>
     7 #include <queue>
     8 #include <vector>
     9 #include <map>
    10 #include <set>
    11 #include <unordered_set>
    12 #include <unordered_map>
    13 #define ll long long
    14 #define fi first
    15 #define se second
    16 #define pb push_back
    17 #define me memset
    18 const int N = 1e6 + 10;
    19 const int mod = 1e9 + 7;
    20 using namespace std;
    21 typedef pair<int,int> PII;
    22 typedef pair<long,long> PLL;
    23 
    24 ll n,x;
    25 ll ans=1;
    26 unordered_map<int,int> Hash;
    27 void divide(ll x){
    28     for(ll i=2;i<=x/i;++i){
    29         if(x%i==0) {
    30             while (x % i == 0) {
    31                 x/=i;
    32                 Hash[i]++;
    33             }
    34         }
    35     }
    36     if(x>1) Hash[x]++;
    37 }
    38 
    39 void sum(){
    40     unordered_map<int,int>::iterator iter;
    41      for(iter=Hash.begin();iter!=Hash.end();++iter){
    42          ans=ans*(iter->se+1)%mod;
    43      }
    44      printf("%lld\n",ans);
    45 }
    46 
    47 
    48 
    49 int main() {
    50     ios::sync_with_stdio(false);
    51     cin>>n;
    52      while(n--){
    53          cin>>x;
    54           divide(x);
    55      }
    56      sum();
    57 
    58     return 0;
    59 }

    求约数和:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <stack>
     7 #include <queue>
     8 #include <vector>
     9 #include <map>
    10 #include <set>
    11 #include <unordered_set>
    12 #include <unordered_map>
    13 #define ll long long
    14 #define fi first
    15 #define se second
    16 #define pb push_back
    17 #define me memset
    18 const int N = 1e6 + 10;
    19 const int mod = 1e9 + 7;
    20 using namespace std;
    21 typedef pair<int,int> PII;
    22 typedef pair<long,long> PLL;
    23 
    24 int n,x;
    25 unordered_map<int,int> Hash;
    26 ll ans=1;
    27 
    28 void divide(int x){
    29     for(int i=2;i<=x/i;++i){
    30         while(x%i==0){
    31             x/=i;
    32             Hash[i]++;
    33         }
    34     }
    35     if(x>1) Hash[x]++;
    36 }
    37 
    38 void sum(){
    39     unordered_map<int,int>::iterator iter;
    40      for(iter=Hash.begin();iter!=Hash.end();++iter){
    41          ll tmp=1;
    42          while(iter->se--){
    43              tmp=(tmp*iter->fi+1)%mod;
    44          }
    45          ans=ans*tmp%mod;
    46      }
    47      printf("%lld\n",ans);
    48 }
    49 
    50 int main() {
    51     ios::sync_with_stdio(false);
    52     cin>>n;
    53      while(n--){
    54          cin>>x;
    55           divide(x);
    56      }
    57     sum();
    58 
    59     return 0;
    60 }
  • 相关阅读:
    Java实现One-way traffic(单向交通)
    Java实现One-way traffic(单向交通)
    Java实现One-way traffic(单向交通)
    Java实现One-way traffic(单向交通)
    C#调用Delphi Dll返回字符串的示例(使用Move才能拷贝字符串)
    Delphi实现菜单项上出现提示
    WebBrowser中获得脚本中的变量值
    比较两个文件是否相同(比较两个流是否相等)
    WebBrowser执行脚本和调用外部方法
    c#之函数创建和闭包
  • 原文地址:https://www.cnblogs.com/lr599909928/p/12640811.html
Copyright © 2011-2022 走看看