其他分享
首页 > 其他分享> > 约数之和(两种方法)

约数之和(两种方法)

作者:互联网

image

也可以直接用等比树列求和公式,乘上逆元

 

 

分治:

  1 #include <iostream>
  2 #include <algorithm>
  3 #include <cstring>
  4 using namespace std;
  5 
  6 using ll = long long;
  7 const int Mod = 9901;
  8 int ksm(int a, int n){
  9     int res = 1;
 10     while(n){
 11         if(n&1) res = (res%Mod * a % Mod) % Mod;
 12         a = a % Mod * a % Mod;
 13         n >>= 1;
 14     }
 15     return res;
 16 }
 17 
 18 int sum(int p, int n){
 19     if(n == 0) return 1;
 20     if(n&1) return ((1 + ksm(p, (n+1)/2)) % Mod * sum(p, (n-1)/2)%Mod ) % Mod;
 21     else return ((1 + ksm(p, n/2))% Mod * sum(p, n/2 - 1)%Mod * ksm(p, n) % Mod)% Mod;
 22 }
 23 
 24 int main(){
 25     int A, B;
 26     int ans = 0;
 27     cin >> A >> B;
 28     for(int i = 2; i <= A; ++ i){
 29         int res = 0;
 30         while(A % i == 0)res++, A /= i;
 31         if(res) ans = (ans % Mod * sum(i, res * B) % Mod)%Mod;
 32     }
 33     cout << ans << endl;
 34     return 0;
 35 }

等比数列求和+逆元:

  1 /*
  2 Sn = (anq - a1)/(q-1)
  3 由本题知q = i;
  4 由于有n+1项;
  5 Sn+1 = (an+1q - a1)/(q-1)
  6 因为an = a1q^(n-1)
  7 得
  8 Sn+1 = (a1q^(n+1) - a1)/(q-1);
  9 在由费马小定理
 10 除以一个数并取模,等于乘上这个树得逆元,在取模;
 11 求逆元用快速幂
 12 ksm(x, Mod - 1)
 13 */
 14 
 15 #include <iostream>
 16 #include <algorithm>
 17 #include <cstring>
 18 using namespace std;
 19 const int Mod = 9901;
 20 using LL = long long;
 21 LL ksm(LL a, LL n){
 22     LL res = 1;
 23     while(n){
 24         if(n&1) res = (res%Mod * a%Mod)%Mod;
 25         a = (a%Mod * a%Mod) % Mod;
 26         n >>= 1;
 27     }
 28     return res;
 29 }
 30 
 31 int main(){
 32     LL A, B;
 33     cin >> A >> B;
 34     LL res = 1;
 35     if(A == 0) res = 0;
 36     for(int i = 2; i <= A; ++ i){
 37     if(A % i == 0){
 38         LL s = 0;
 39         while(A % i == 0){
 40             s ++;
 41             A /= i;
 42         }
 43 
 44         LL x = i % Mod;
 45         if(i % Mod == 1)//避免出现为零的情况余数为1的时候逆元为零,不满足条件
 46             res = res*(B*s + 1) % Mod;
 47         else res = (res % Mod * (ksm(x, s * B + 1)-1) % Mod * ksm(x - 1, Mod - 2) %Mod) %Mod;
 48 
 49         }
 50     }
 51     cout  << res << endl;
 52     return 0;
 53 }

标签:约数,两种,int,res,LL,ksm,include,方法,Mod
来源: https://www.cnblogs.com/rstz/p/12686907.html