【疯狂gcd】的推式子过程
∑ i = 1 n ∑ j = 1 n g c d ( i , j ) \sum_{i=1}^n\sum_{j=1}^ngcd(i,j) ∑i=1n∑j=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 =2∑i=1n∑j=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)=∑d∣nd∑i=d,d∣in[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)=∑d∣nd∑j=1dn[gcd(j,dn)==1]
f ( n ) = ∑ d ∣ n d φ ( n d ) f(n)=\sum_{d|n}d\varphi(\frac nd) f(n)=∑d∣ndφ(dn)
∑ i = 1 n ∑ j = 1 i g c d ( i , j ) \sum_{i=1}^n\sum_{j=1}^igcd(i,j) ∑i=1n∑j=1igcd(i,j)
= ∑ i = 1 n ∑ d ∣ i d φ ( i d ) =\sum_{i=1}^n\sum_{d|i}d\varphi(\frac id) =∑i=1n∑d∣idφ(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=1nd∑i=1,d∣inφ(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=1nd∑j=1⌊dn⌋φ(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′+(a−kb)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(x′−ky′)=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=x′−ky′
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 My1−my2=a−A
M y 1 = a − A m o d m My_1=a-A \mod m My1=a−Amodm
设 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=da−Amoddm
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 0≤i≤k,mp是一个map容器)
枚举 i i i,计算 a × g − i k m o d p a×g^{-ik} \mod p a×g−ikmodp
若存在 j j j,使 m p [ a × g − i k m o d p ] = j mp[a×g^{-ik}\mod p]=j mp[a×g−ikmodp]=j,则 a × g − i k = g j m o d p a×g^{-ik}=g^j \mod p a×g−ik=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 15−05+25−15+⋯+n5−(n−1)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=1ni5−∑i=1n(i−1)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=1ni5−∑i=1n(i5−C51i4+C52i3−C53i2+C54i−1)
= 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 =C51∑i=1ni4−C52∑i=1ni3+C53∑i=1ni2−C54∑i=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+C52∑i=1ni3−C53∑i=1ni2+C54∑i=1ni−1
一般地: