数学基础

1. 等差数列

  1. an=a1+(n1)×da_n = a_1 + (n-1) \times d
  2. Sn=na1+n(n1)d2,nNS_n = na_1 + \frac{n(n-1)d}{2}, n \in N^*

其中,a1a_1 为首项,ana_n 为末项,公差为d,前n项和为SnS_n

2. 等比数列

  1. an=a1q(n1)a_n = a_1 \cdot q^{(n-1)}
  2. Sn=a1(1qn)1q(q1)S_n = \frac{a_1(1-q^n)}{1-q}(q\neq1)

其中,a1a_1 为首项,ana_n 为末项,公比为d,前n项和为SnS_n

3. 取模运算

3.1 四则运算(除法除外)

  1. (a+b)%p=(a%p+b%p)%p(a + b) \% p = (a \% p + b \% p) \% p
  2. (ab)%p=(a%pb%p)%p(a – b) \% p = (a \% p – b \% p) \% p
  3. (ab)%p=(a%pb%p)%p(a * b) \% p = (a \% p * b \% p) \% p
  4. (ab)%p=((a%p)b)%p(a^b) \% p = ((a \% p)^b) \% p

3.2 结合律

  1. ((a+b)%p+c)%p=(a+(b+c)%p)%p((a + b) \% p + c) \% p = (a + (b+c) \% p) \% p
  2. ((a×b)%p×c)%p=(a×(b×c)%p)%p((a \times b) \% p \times c) \% p = (a \times (b \times c) \% p) \% p

3.3 交换律

  1. (a+b)%p=(b+a)%p(a + b) \% p = (b+a) \% p
  2. (a×b)%p=(b×a)%p(a \times b) \% p = (b \times a) \% p

3.4 分配律

  1. ((a+b)%p×c)%p=((a×c)%p+(b×c)%p)%p((a + b) \% p \times c) \% p = ((a \times c) \% p + (b \times c) \% p) \% p

3.5 重要定理

  1. ab(%p)a≡b (\% p),则对于任意的c,都有(a+c)(b+c)(%p)(a + c) ≡ (b + c) (\%p)
  2. ab(%p)a≡b (\% p),则对于任意的c,都有(a×c)(b×c)(%p)(a \times c) ≡ (b \times c) (\%p)
  3. ab(%p)cd(%p)a≡b (\% p) ,c≡d (\% p),则
    1. (a+c)(b+d)(%p)(a + c) ≡ (b + d) (\%p)
    2. (ac)(bd)(%p)(a – c) ≡ (b – d) (\%p)
    3. (a×c)(b×d)(%p)(a \times c) ≡ (b \times d) (\%p)
    4. (a/c)(b/d)(%p)(a / c) ≡ (b / d) (\%p)

4. 幂指

ans=basepowerans=base^{power},例如210000002^{1000000}

4.1 递推

long long power1(long long base,long long power,long long mod){
	long long result = 1;
	
	for(int i=1;i<=power;i++)
		result = ((result%mod)*(base%mod))%mod;
	
	return result;
}

4.2 快速幂

  1. 310=3×3×3×3×3×3×3×3×3×33^{10}=3\times3\times3\times3\times3\times3\times3\times3\times3\times3
  2. 310=(3×3)×(3×3)×(3×3)×(3×3)×(3×3)3^{10}=(3\times3)\times(3\times3)\times(3\times3)\times(3\times3)\times(3\times3)
  3. 310=(3×3)53^{10}=(3\times3)^5
  4. 310=953^{10}=9^{5}
  5. 95=94×919^{5}=9^{4}\times9^{1}
  6. 95=812×919^{5}=81^{2}\times9^{1}
  7. 95=56611×919^{5}=5661^{1}\times9^{1}
long long power2(long long base,long long power,long long mod){
	long long result = 1;
	
	while(power>0){
		if(power%2==0){
			power = power/2;
			base = ((base%mod)*(base%mod))%mod;
		}else{
			power = power -1;
			result = ((result%mod)*(base%mod))%mod;
			power = power/2;
			base = ((base%mod)*(base%mod))%mod;
		}
	}	
	return result;
}
long long power3(long long base,long long power,long long mod){
	long long result = 1;
	
	while(power>0){
		if(power%2==1){
			result = ((result%mod)*(base%mod))%mod;
		}
		power = power/2;
		base = ((base%mod)*(base%mod))%mod;
	}
		
	return result;
}
long long power4(long long base,long long power,long long mod){
	long long result = 1;
	
	while(power>0){
		if(power&1){
			result = ((result%mod)*(base%mod))%mod;
		}
		power >>=1;
		base = ((base%mod)*(base%mod))%mod;
	}
		
	return result;
}

5. 约数个数和约数和

5.1 基本定理

任何大于1的整数A都可以分解成若干质数的乘积。如果不考虑这些质数次序,A可以写成标准分解式:
A=P1a1×P2a2×P3a3××PnanA=P_1^{a1} \times P_2^{a2} \times P_3^{a3} \times \cdots \times P_n^{an}
其中 P1<P1<P1<<PnP_1 \lt P_1 \lt P_1 \lt \ldots \lt P_n 为质数,aia_i为非负整数i=1,2,,ni = 1,2,\cdots,n

5.2 约数个数公式

A=P1a1×P2a2×P3a3××PnanA=P_1^{a1} \times P_2^{a2} \times P_3^{a3} \times \cdots \times P_n^{an} 为标准公式,则A的所有约数(包括1和本身)的个数等于:
(a1+1)×(a2+1)×(a3+1)××(an+n)(a_1+1)\times (a_2+1)\times (a_3+1) \times \cdots \times (a_n+n)

5.3 约数和公式

A=P1a1×P2a2×P3a3××PnanA=P_1^{a1} \times P_2^{a2} \times P_3^{a3} \times \cdots \times P_n^{an} 为标准公式,则A的所有约数(包括1和本身)的和等于:
(1+p1+p12++p1a1)×(1+p2+p22++p2a2)×(1+p3+p32++p3a3)××(1+pn+pn2++pnan)(1+p_1+p_1^2+\cdots+p_1^{a1}) \times (1+p_2+p_2^2+\cdots+p_2^{a2}) \times (1+p_3+p_3^2+\cdots+p_3^{a3}) \times \cdots \times (1+p_n+p_n^2+\cdots+p_n^{an})

  1. 递推求和O(n)
    #include <iostream>
    using namespace std;
    
    int a1=1,q,n,sum=1,mod=9901,acc=1;
    
    int main(){
    
    	cin >> q >> n;
    	for(int i=1;i<=n;i++){
    		acc=((acc%mod)*q)%mod;
    		sum = (sum%mod + acc%mod)%mod;
    	}
    	cout << sum << endl;
    	
    	return 0; 
    }
    
  2. 分治求和O(log2n)
    1. n为奇数时
      (1+p1+p12+p13+p14+p15+p16+p17)%9901(1+p_1+p_1^2+p_1^3+p_1^4+p_1^5+p_1^6+p_1^7)\%9901
      =(1+p1+p12+p13+p14(1+p1+p12+p13))%9901=(1+p_1+p_1^2+p_1^3+p_1^4(1+p_1+p_1^2+p_1^3))\%9901
      =(1+p1+p12+p13)继续递归×(1+p14)直接计算%9901=\underbrace{(1+p_1+p_1^2+p_1^3)}_{继续递归} \times \underbrace{(1+p_1^4)}_{直接计算}\%9901
    2. n为偶数时
      (1+p1+p12+p13+p14+p15+p16)%9901(1+p_1+p_1^2+p_1^3+p_1^4+p_1^5+p_1^6)\%9901
      =(1+p1+p12)+p13+p14(1+p1+p12))%9901=(1+p_1+p_1^2)+p_1^3+p_1^4(1+p_1+p_1^2))\%9901
      =((1+p1+p12)继续递归×(1+p14)直接计算+p13直接计算)%9901=(\underbrace{(1+p_1+p_1^2)}_{继续递归} \times \underbrace{(1+p_1^4)}_{直接计算}+\underbrace{p_1^3}_{直接计算})\%9901
    #include <iostream>
    #include "power4.h"
    using namespace std;
    
    long long sum(long long a, long long n,long long mod){
    	if(n ==1) return a;
    	long long t = sum(a,n/2,mod);
    	if(n&1){
    		long long cur = power4(a,n/2+1,mod);
    		t = (t+t*cur%mod)%mod;
    		t = (t+cur)%mod;
    	}else{
    		long long cur = power4(a,n/2,mod);
    		t=(t+t*cur%mod) % mod;
    	}
    	return t;
    }
    
    int main(){
    	
    	cout << 1+sum(2,3,9901);
    	
    	return 0;
    }
    

5.4 分解质因数

5.5 POJ1845 Sumdiv

Consider two natural numbers A and B. Let S be the sum of all natural divisors of ABA^B.

Input

The only line contains the two natural numbers A and B, (0 <= A,B <= 500000000) separated by blanks.

Output

The only line of the output will contains S modulo 9001.

Sample Input

2 3

Sample Output

15

HINT

23=82^3 = 8

The natural divisors of 8 are: 1,2,4,8. Their sum is 15.

15 modulo 9901 is 15 (that should be output).

Analysis

(1+p1+p12++p1a1B)×(1+p2+p22++p2a2B)×(1+p3+p32++p3a3B)××(1+pn+pn2++pnanB)(1+p_1+p_1^2+\cdots+p_1^{a_1B}) \times (1+p_2+p_2^2+\cdots+p_2^{a_2B}) \times (1+p_3+p_3^2+\cdots+p_3^{a_3B}) \times \cdots \times (1+p_n+p_n^2+\cdots+p_n^{a_nB})

Code