【疯狂gcd】的推式子过程

∑ i = 1 n ∑ j = 1 n g c d ( i , j ) \sum_{i=1}^n\sum_{j=1}^ngcd(i,j) i=1nj=1ngcd(i,j)

= 2 ∑ i = 1 n ∑ j = 1 i g c d ( i , j ) − ∑ 1 = 1 n i =2\sum_{i=1}^n\sum_{j=1}^igcd(i,j)-\sum_{1=1}^ni =2i=1nj=1igcd(i,j)1=1ni

f ( n ) = ∑ j = 1 n g c d ( n , j ) f(n)=\sum_{j=1}^ngcd(n,j) f(n)=j=1ngcd(n,j)

f ( n ) = ∑ d ∣ n d ∑ i = d , d ∣ i n [ g c d ( i , n ) = = d ] f(n)=\sum_{d|n}d\sum_{i=d,d|i}^n[gcd(i,n)==d] f(n)=dndi=d,din[gcd(i,n)==d]

(令 i = j d i=jd i=jd

f ( n ) = ∑ d ∣ n d ∑ j = 1 n d [ g c d ( j , n d ) = = 1 ] f(n)=\sum_{d|n}d\sum_{j=1}^{\frac nd}[gcd(j,\frac nd)==1] f(n)=dndj=1dn[gcd(j,dn)==1]

f ( n ) = ∑ d ∣ n d φ ( n d ) f(n)=\sum_{d|n}d\varphi(\frac nd) f(n)=dndφ(dn)

∑ i = 1 n ∑ j = 1 i g c d ( i , j ) \sum_{i=1}^n\sum_{j=1}^igcd(i,j) i=1nj=1igcd(i,j)

= ∑ i = 1 n ∑ d ∣ i d φ ( i d ) =\sum_{i=1}^n\sum_{d|i}d\varphi(\frac id) =i=1ndidφ(di)

= ∑ d = 1 n d ∑ i = 1 , d ∣ i n φ ( i d ) =\sum_{d=1}^nd\sum_{i=1,d|i}^n\varphi(\frac id) =d=1ndi=1,dinφ(di)

(令 i = j d i=jd i=jd

= ∑ d = 1 n d ∑ j = 1 ⌊ n d ⌋ φ ( j ) =\sum_{d=1}^nd\sum_{j=1}^{\lfloor \frac nd \rfloor}\varphi(j) =d=1ndj=1dnφ(j)

exgcd

a x + b y = 1 ax+by=1 ax+by=1

b x ′ + ( a m o d    b ) y ′ = 1 bx'+(a\mod b)y'=1 bx+(amodb)y=1

b x ′ + ( a − k b ) y ′ = 1 bx'+(a-kb)y'=1 bx+(akb)y=1

其中, k = ⌊ a b ⌋ k=\lfloor \frac ab \rfloor k=ba

a y ′ + b ( x ′ − k y ′ ) = 1 ay'+b(x'-ky')=1 ay+b(xky)=1

x ′ , y ′ x',y' x,y已知(最后一步是 x ′ = 1 , y ′ = 0 x'=1,y'=0 x=1,y=0递归推导前面的 x x x y y y

x = y ′ , y = x ′ − k y ′ x=y',y=x'-ky' x=y,y=xky

void exgcd(int a,int b,int& x,int& y){
   
    if(!b){
   x=1,y=0;return;}
    exgcd(b,a%b,x,y);
    int xx=x,yy=y;
    x=yy,y=xx-a/b*yy;
    //简便但不是很容易理解的写法:exgcd(b,a%b,y,x),y-=a/b*x;
}

exCRT

前面几个方程解出的结果为: x = A m o d    M x=A \mod M x=AmodM

新的方程: x = a m o d    m x=a \mod m x=amodm

设:

x = A + M y 1 x=A+My_1 x=A+My1

x = a + m y 2 x=a+my_2 x=a+my2

两式相减:

M y 1 − m y 2 = a − A My_1-my_2=a-A My1my2=aA

M y 1 = a − A m o d    m My_1=a-A \mod m My1=aAmodm

d = gcd ⁡ ( M , m ) d=\gcd(M,m) d=gcd(M,m)

M d y 1 = a − A d m o d    m d \frac Mdy_1=\frac{a-A}d \mod \frac md dMy1=daAmoddm

exgcd \text{exgcd} exgcd解出 y 1 y_1 y1后得

x = A + M y 1 m o d    l c m ( M , m ) x=A+My_1 \mod lcm(M,m) x=A+My1modlcm(M,m)

int n;
ll exgcd(ll a,ll b,ll& x,ll& y){
   
	if(!b)return x=1,y=0,a;
	ll d=exgcd(b,a%b,y,x);
	y-=x*(a/b);
	return d;
}
int main(){
   
	while(~scanf("%d",&n)){
   
		ll M=read(),x0=read(),mi,ai,c,t;
		for(int i=2;i<=n;++i){
   
			ll mi=read(),ai=read(),x,y;
			if(x0==-1)continue;
			ll c=(ai-x0%mi+mi)%mi,d=exgcd(M,mi,x,y);
			ll t=mi/d;
			if(c%d){
   x0=-1;continue;}
			x=(__int128)x*c/d%t;
			x0+=M*x,M*=t,x0=(x0%M+M)%M;
		}
		enter(x0);
	}
}

BSGS

g x = a m o d    p g^x=a \mod p gx=amodp

令$k=\lfloor \sqrt p\rfloor $

m p [ g i m o d    p ] = i mp[g^i \mod p]=i mp[gimodp]=i 0 ≤ i ≤ k , m p 0\leq i\leq k,mp 0ik,mp是一个map容器)

枚举 i i i,计算 a × g − i k m o d    p a×g^{-ik} \mod p a×gikmodp

若存在 j j j,使 m p [ a × g − i k m o d    p ] = j mp[a×g^{-ik}\mod p]=j mp[a×gikmodp]=j,则 a × g − i k = g j m o d    p a×g^{-ik}=g^j \mod p a×gik=gjmodp,即 a = g i k + j m o d    p a=g^{ik+j} \mod p a=gik+jmodp

x = i k + j x=ik+j x=ik+j

∑ i = 1 n i k \sum_{i=1}^ni^k i=1nik

∑ i = 1 n i 4 \sum_{i=1}^ni^4 i=1ni4???

1 5 − 0 5 + 2 5 − 1 5 + ⋯ + n 5 − ( n − 1 ) 5 = n 5 1^5-0^5+2^5-1^5+\dots+n^5-(n-1)^5=n^5 1505+2515++n5(n1)5=n5

n 5 = ∑ i = 1 n i 5 − ∑ i = 1 n ( i − 1 ) 5 n^5=\sum_{i=1}^ni^5-\sum_{i=1}^n(i-1)^5 n5=i=1ni5i=1n(i1)5

= ∑ i = 1 n i 5 − ∑ i = 1 n ( i 5 − C 5 1 i 4 + C 5 2 i 3 − C 5 3 i 2 + C 5 4 i − 1 ) =\sum_{i=1}^ni^5-\sum_{i=1}^n(i^5-C_5^1i^4+C_5^2i^3-C_5^3i^2+C_5^4i-1) =i=1ni5i=1n(i5C51i4+C52i3C53i2+C54i1)

= C 5 1 ∑ i = 1 n i 4 − C 5 2 ∑ i = 1 n i 3 + C 5 3 ∑ i = 1 n i 2 − C 5 4 ∑ i = 1 n i + 1 =C_5^1\sum_{i=1}^ni^4-C_5^2\sum_{i=1}^ni^3+C_5^3\sum_{i=1}^ni^2-C_5^4\sum_{i=1}^ni+1 =C51i=1ni4C52i=1ni3+C53i=1ni2C54i=1ni+1

∑ i = 1 n i 4 = n 5 + C 5 2 ∑ i = 1 n i 3 − C 5 3 ∑ i = 1 n i 2 + C 5 4 ∑ i = 1 n i − 1 5 \sum_{i=1}^ni^4=\frac{n^5+C_5^2\sum_{i=1}^ni^3-C_5^3\sum_{i=1}^ni^2+C_5^4\sum_{i=1}^ni-1}5 i=1ni4=5n5+C52i=1ni3C53i=1ni2+C54i=1ni1

一般地:

∑ i = 1 n i k = n k + 1 + ∑ j = 2 k C k + 1 j ( − 1 ) j ∑ i = 1 n i k + 1 − j k + 1 \sum_{i=1}^ni^k=\frac{n^{k+1}+\sum_{j=2}^kC_{k+1}^j(-1)^j\sum_{i=1}^ni^{k+1-j}}{k+1} i=1nik=k+1nk+1+j=2kCk+1j(1)ji=1nik+1j