当前位置:网站首页>[noi simulation] pendulum (linear algebra, Du Jiao sieve)

[noi simulation] pendulum (linear algebra, Du Jiao sieve)

2022-06-24 08:59:00 DD(XYX)

Topic

6s , 1024mb

I am a XYX, I'm good at swinging .

I saw one when I was putting it on n × n n\times n n×n Matrix A A A
A i , j = { 1 i = j 0 i ≠ j ∧ i ∣ j C o t h e r w i s e A_{i,j}=\begin{cases} 1 & i=j\\ 0 & i\not=j\land i|j\\ C & {\rm otherwise} \end{cases} Ai,j=10Ci=ji=jijotherwise

Now I want to know A A A The determinant of , Because I put , So the answer just needs to be right 998244353 998244353 998244353 modulus .

1 ≤ n ≤ 1 0 11 , 0 ≤ C < 998244353 1\leq n\leq10^{11},0\leq C<998244353 1n1011,0C<998244353 .

Examples

6 2 → \rightarrow 998244350

99 4 → \rightarrow 714389845

10000000000 10 → \rightarrow 82177551

Answer key

This problem tells us , It is not necessary to construct upper triangular or lower triangular matrix to calculate determinant .

We can also construct Heisenberg matrix relatively easily .

This is the matrix A A A
[ 1 0 0 0 0 … C 1 C 0 C … C C 1 C C … C C C 1 C … C C C C 1 … … … … … … ⋱ ] \left[\begin{matrix} 1 & 0 & 0 & 0 &0&\ldots\\ C & 1 & C & 0 & C&\ldots\\ C & C & 1 & C & C&\ldots\\ C & C & C & 1 & C&\ldots\\ C & C & C & C & 1 &\ldots\\ \ldots&\ldots&\ldots&\ldots&\ldots&\ddots \end{matrix}\right] 1CCCC01CCC0C1CC00C1C0CCC1

Let's put the matrix A A A Subtract the upper line from each line ( Start from below , Subtract the previous line from each line , The first line doesn't move ), We can get a Shanghai senberg matrix :
[ 1 0 0 0 0 … C − 1 1 C 0 C … 0 C − 1 1 − C C 0 … 0 0 C − 1 1 − C 0 … 0 0 0 C − 1 1 − C … … … … … … ⋱ ] \left[\begin{matrix} 1 & 0 & 0 & 0 &0&\ldots\\ C-1 & 1 & C & 0 & C&\ldots\\ 0 & C-1 & 1-C & C & 0&\ldots\\ 0 & 0 & C-1 & 1-C & 0&\ldots\\ 0 & 0 & 0 & C-1 & 1-C &\ldots\\ \ldots&\ldots&\ldots&\ldots&\ldots&\ddots \end{matrix}\right] 1C100001C1000C1CC1000C1CC10C001C

Each permutation ring of the matrix is a counter clockwise ring with continuous labels , Be similar to i → j → j − 1 → . . . → i + 1 i\rightarrow j\rightarrow j-1 \rightarrow...\rightarrow i+1 ijj1...i+1 Of . Differential obtainable , Each multiple relationship in the upper right i ∣ j i|j ij , Will give A i , j A_{i,j} Ai,j contribution − C -C C , to A i + 1 , j A_{i+1,j} Ai+1,j contribution C C C. We multiply each position of the matrix by 1 1 − C \frac{1}{1-C} 1C1 , Then make f i f_i fi Before retention i i i That's ok i i i Determinant of column , that
f i = f i − 1 + C 1 − C ∑ j ∣ i , j < i ( f j − f j − 1 ) f_i=f_{i-1}+\frac{C}{1-C}\sum_{j|i,j<i} (f_j-f_{j-1}) fi=fi1+1CCji,j<i(fjfj1)

set up g i = f i − f i − 1 g_i=f_i-f_{i-1} gi=fifi1 , that g i = C 1 − C ∑ j ∣ i , j < i g j g_i=\frac{C}{1-C}\sum_{j|i,j<i}g_j gi=1CCji,j<igj .

So what we want is g i g_i gi And f n f_n fn , The data range should be a sub linear sieve .

Make h 1 = − 1 , h i = C 1 − C h_1=-1,h_{i}=\frac{C}{1-C} h1=1,hi=1CC , that ( Dirichlet convolution ) h ∗ g = − e h*g=-e hg=e , So you can Dujiao sieve ( Duchenne sieve is not restricted to be a product function ).
∑ i = 1 n ∑ j ∣ i h j g i / j = ∑ j n h j ∑ i n / j g i = − 1 ⇒ f n = 1 + ∑ j = 2 n h j f n / j \sum_{i=1}^{n}\sum_{j|i}h_jg_{i/j}=\sum_{j}^nh_j\sum_{i}^{n/j}g_i=-1\\ \Rightarrow f_n=1+\sum_{j=2}^nh_jf_{n/j} i=1njihjgi/j=jnhjin/jgi=1fn=1+j=2nhjfn/j

We preprocess n 2 / 3 n^{2/3} n2/3 Inside f i f_i fi , So the complexity of Du Jiao sieve is O ( n 2 / 3 ) O(n^{2/3}) O(n2/3) .

But preprocessing can easily bring a log ⁡ \log log , So we need to optimize .

Make x = ∏ p i w i x=\prod p_i^{w_i} x=piwi , We put p i p_i pi Change from small to large 2 , 3 , 5 , 7 , . . . 2,3,5,7,... 2,3,5,7,... obtain y = ∏ p ′ i w i y=\prod p{'}_i^{w_i} y=piwi , Easy to find g x = g y g_x=g_y gx=gy , because g g g Only follow w i w_i wi On the resettable set of . We use Euler sieve to find the maximum prime factor and the number of prime factors for each number , And then recursively calculate each x x x Corresponding y y y . Violence finds something fundamentally different y y y Only a thousand at most , We can each O ( n 1 / 3 ) O(n^{1/3}) O(n1/3) Find out g g g .

Be careful , Because every position of the matrix is multiplied by 1 1 − C \frac{1}{1-C} 1C1 , And because the upper left corner of the matrix is not standard , So the final answer is
f n ⋅ ( 1 − C ) n − 1 f_n\cdot (1-C)^{n-1} fn(1C)n1

The sum O ( n 2 / 3 ) O(n^{2/3}) O(n2/3) .

CODE

#include<map>
#include<set>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<random>
#include<bitset>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#pragma GCC optimize(2)
using namespace std;
#define MAXN 10000005
#define LL long long
#define ULL unsigned long long
#define ENDL putchar('\n')
#define DB double
#define lowbit(x) (-(x) & (x))
#define FI first
#define SE second
#define PR pair<int,int>
int xchar() {
    
	static const int maxn = 1000000;
	static char b[maxn];
	static int pos = 0,len = 0;
	if(pos == len) pos = 0,len = fread(b,1,maxn,stdin);
	if(pos == len) return -1;
	return b[pos ++];
}
// #define getchar() xchar()
LL read() {
    
	LL f = 1,x = 0;int s = getchar();
	while(s < '0' || s > '9') {
    if(s<0)return -1;if(s=='-')f=-f;s = getchar();}
	while(s >= '0' && s <= '9') {
    x = (x<<1) + (x<<3) + (s^48);s = getchar();}
	return f*x;
}
void putpos(LL x) {
    if(!x)return ;putpos(x/10);putchar((x%10)^48);}
void putnum(LL x) {
    
	if(!x) {
    putchar('0');return ;}
	if(x<0) putchar('-'),x = -x;
	return putpos(x);
}
void AIput(LL x,int c) {
    putnum(x);putchar(c);}

const int MOD = 998244353;
int n,m,s,o,k;
LL N;
inline int qkpow(int a,int b) {
    
	int res = 1;
	while(b > 0) {
    
		if(b & 1) res = res *1ll* a % MOD;
		a = a *1ll* a % MOD; b >>= 1;
	} return res;
}
int V;
int sg[MAXN],lw[MAXN],ct[MAXN],hb[MAXN];
int p[MAXN],cnt; bool f[MAXN];
void sieve(int n) {
    
	sg[1] = 1; lw[1] = 1; int nm = 0;
	for(int i = 2;i <= n;i ++) {
    
		if(!f[i]) p[++ cnt] = i,lw[i] = 2,hb[i] = i,ct[i] = 1;
		if(lw[i] == i) {
    
			for(int j = 1;j * j <= i;j ++) {
    
				if(i % j == 0) {
    
					(sg[i] += sg[j]) %= MOD;
					if(i/j > j && j > 1) {
    
						(sg[i] += sg[i/j]) %= MOD;
					}
				}
			}
			sg[i] = sg[i] *1ll* V % MOD;
		}
		else sg[i] = sg[lw[i]];
		for(int j = 1;j <= cnt && (nm=i*p[j]) <= n;j ++) {
    
			f[nm] = 1; hb[nm] = hb[i];
			if(i % p[j] == 0) {
    
				ct[nm] = ct[i];
				lw[nm] = lw[nm/hb[nm]] * p[ct[nm]];
				break;
			}
			ct[nm] = ct[i] + 1;
			lw[nm] = lw[nm/hb[nm]] * p[ct[nm]];
		}
	}
	for(int i = 2;i <= n;i ++) {
    
		(sg[i] += sg[i-1]) %= MOD;
	}
	return ;
}
int sh(LL x) {
    
	if(x<1) return 0;
	return ((x-1)%MOD*V%MOD +MOD-1) % MOD;
}
int s1[1000005];
int Sg(LL x) {
    
	if(x <= 10000000) return sg[x];
	int &rs = s1[N/x];
	if(rs >= 0) return rs;
	rs = 1;
	for(LL i = 2;i <= x;i ++) {
    
		LL r = x/(x/i);
		rs += (r-i+1)%MOD*V%MOD *1ll* Sg(x/i) % MOD;
		if(rs >= MOD) rs -= MOD;
		i = r;
	} return rs;
}
int main() {
    
	freopen("bigben.in","r",stdin);
	freopen("bigben.out","w",stdout);
	N = read(); int C = read();
	if(C <= 1) return AIput(N<=2,'\n'),0;
	V = C *1ll* qkpow(MOD+1-C,MOD-2) % MOD;
	sieve(10000000);
	memset(s1,-1,sizeof(s1));
	int as = Sg(N) *1ll* qkpow(MOD+1-C,(N-1)%(MOD-1)) % MOD;
	AIput(as,'\n');
	return 0;
}
原网站

版权声明
本文为[DD(XYX)]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240626417595.html