1. 程式人生 > 其它 >DZY Loves Math IV(杜教篩)

DZY Loves Math IV(杜教篩)

技術標籤:# 杜教篩,min_25篩杜教篩數學

文章目錄

title

solution

這道題是多麼的妙啊,完全不是我能推出來的式子呢!
在這裡插入圖片描述
觀察資料範圍,有點奇怪欸,在暗示我??
在這裡插入圖片描述
考慮暴力列舉 n n n
S ( n , m ) = ∑ i = 1 m φ ( n × i ) S(n,m)=\sum_{i=1}^mφ(n\times i) S(n,m)=i=1mφ(n×i)
神奇的操作來了,將 n n n質因數分解,並把不同的質因數分別拿出一個
n = ∏ p i e i n=\prod p_i^{e_i}

n=piei
q = ∏ p i q=\prod p_i q=pi
p = ∏ p i e i − 1 p=\prod p_i^{e_i-1} p=piei1
則有 p × q = n p\times q=n p×q=n

  1. i % j = 0 i\% j=0 i%j=0,則 φ ( i j ) = φ ( i ) × j φ(ij)=φ(i)\times j φ(ij)=φ(i)×j
  2. ( i , j ) = 1 (i,j)=1 (i,j)=1,則 φ ( i j ) = φ ( i ) × φ ( j ) φ(ij)=φ(i)\times φ(j)
    φ(ij)=φ(i)×φ(j)

S ( n , m ) = ∑ i = 1 m φ ( n × i ) S(n,m)=\sum_{i=1}^mφ(n\times i) S(n,m)=i=1mφ(n×i) = p ⋅ ∑ i = 1 m φ ( q × i ) =p\ ·\sum_{i=1}^mφ(q\times i) =pi=1mφ(q×i) = p ⋅ ∑ i = 1 m φ ( q g c d ( q , i ) × i × g c d ( q , i ) ) =p\ ·\sum_{i=1}^mφ(\frac{q}{gcd(q,i)}\times i\times gcd(q,i))

=pi=1mφ(gcd(q,i)q×i×gcd(q,i)) = p ⋅ ∑ i = 1 m φ ( q g c d ( q , i ) ) φ ( i × g c d ( q , i ) ) =p\ ·\sum_{i=1}^mφ(\frac{q}{gcd(q,i)})φ(i\times gcd(q,i)) =pi=1mφ(gcd(q,i)q)φ(i×gcd(q,i)) = p ⋅ ∑ i = 1 m φ ( q g c d ( q , i ) ) φ ( i ) g c d ( q , i ) =p\ ·\sum_{i=1}^mφ(\frac{q}{gcd(q,i)})φ(i)gcd(q,i) =pi=1mφ(gcd(q,i)q)φ(i)gcd(q,i) = p ∑ i = 1 m φ ( q g c d ( q , i ) ) φ ( i ) ∑ d ∣ g c d ( q , i ) φ ( d ) =p\sum_{i=1}^mφ(\frac{q}{gcd(q,i)})φ(i)\sum_{d|gcd(q,i)}φ(d) =pi=1mφ(gcd(q,i)q)φ(i)dgcd(q,i)φ(d) = p ∑ i = 1 m φ ( i ) ∑ d ∣ i , d ∣ q φ ( q d ) =p\sum_{i=1}^mφ(i)\sum_{d|i,d|q}φ(\frac{q}{d}) =pi=1mφ(i)di,dqφ(dq) = p ∑ d ∣ q φ ( q d ) ∑ i = 1 ⌊ n d ⌋ φ ( i × d ) =p\sum_{d|q}φ(\frac{q}{d})\sum_{i=1}^{\lfloor\frac{n}{d}\rfloor}φ(i\times d) =pdqφ(dq)i=1dnφ(i×d) = p ∑ d ∣ q φ ( q d ) S ( d , ⌊ n d ⌋ ) =p\sum_{d|q}φ(\frac{q}{d})S(d,\lfloor\frac{n}{d}\rfloor) =pdqφ(dq)S(d,dn)
在這裡插入圖片描述
φ φ φ用杜教篩,應該是老熟人了
S ( n , m ) S(n,m) S(n,m)記憶化一下,應該就沒了

code

#include <cstdio>
#include <vector>
#include <map>
using namespace std;
#define mod 1000000007
#define int long long
#define maxn 200000
map < int, int > mp, s[maxn];
int cnt;
int minp[maxn + 5]; //minp[i]:i的最大質因子
int prime[maxn], phi[maxn + 5]; //phi[i]:1~i的phi的字首和 
bool vis[maxn + 5];
 
void init() {
	phi[1] = 1;
	for( int i = 2;i <= maxn;i ++ ) {
		if( ! vis[i] ) prime[++ cnt] = i, minp[i] = i, phi[i] = i - 1;
		for( int j = 1;j <= cnt && i * prime[j] <= maxn;j ++ ) {
			vis[i * prime[j]] = 1, minp[i * prime[j]] = prime[j];
			if( i % prime[j] == 0 ) {
				phi[i * prime[j]] = phi[i] * prime[j] % mod;//與式子推導的第二步為什麼p能直接從φ裡面拿出來呼應
				break;
			}
			else
				phi[i * prime[j]] = phi[i] * ( prime[j] - 1 ) % mod;
		}
	}
	for( int i = 1;i <= maxn;i ++ ) phi[i] = ( phi[i] + phi[i - 1] ) % mod;
}

int Phi( int n ) {
	if( n <= maxn ) return phi[n];
	if( mp[n] ) return mp[n];
	int ans = n * ( n + 1 ) / 2 % mod;
	for( int i = 2, r;i <= n;i = r + 1 ) {
		r = n / ( n / i );
		ans = ( ans - ( r - i + 1 ) * Phi( n / i ) % mod + mod ) % mod;
	}
	return mp[n] = ans;
}

int solve( int n, int m ) {
	if( ! m ) return 0;
	if( s[n][m] ) return s[n][m];
	if( n == 1 ) return s[n][m] = Phi( m );
	if( m == 1 ) return s[n][m] = ( Phi( n ) - Phi( n - 1 ) + mod ) % mod;
	vector < int > g;
	int p = 1, q = 1, N = n, x;
	while( N > 1 ) {
		x = minp[N], q *= x, N /= x, g.push_back( x );
		while( N % x == 0 ) N /= x, p *= x;
	}
	int len = g.size(), ans = 0;
	for( int i = 0;i < ( 1 << len );i ++ ) { //列舉q的所有質因子(狀壓) 
		int d = 1;
		for( int j = 0;j < len;j ++ )
			if( i & ( 1 << j ) ) d = d * g[j]; //二進位制位為1則有該質因子
		ans = ( ans + ( Phi( q / d ) - Phi( q / d - 1 ) + mod ) % mod * solve( d, m / d ) % mod ) % mod;
	}
	return s[n][m] = ans * p % mod;
}

signed main() {
	int n, m;
	scanf( "%lld %lld", &n, &m );
	init();
	int ans = 0;
	for( int i = 1;i <= n;i ++ ) 
		ans = ( ans + solve( i, m ) ) % mod;
	printf( "%lld\n", ans );
	return 0;
}