其他分享
首页 > 其他分享> > Solution -「LOJ 6485」 LJJ 学二项式定理

Solution -「LOJ 6485」 LJJ 学二项式定理

作者:互联网

\(\mathcal{Description}\)

  Link.

  给定 \(n,s,a_0,a_1,a_2,a_3\),求:

\[\sum_{i=0}^n\binom{n}is^ia_{i\bmod4}\bmod998244353 \]

  多测,数据组数 \(\le10^5\),\(n\le10^{18}\),其余输入 \(\le10^8\)。

\(\mathcal{Solution}\)

  单位根反演板题。记一个函数 \(f\) 有:

\[\begin{aligned} f(x)&=\sum_{i=0}^n\binom{n}is^ix^i\\ &=(sx+1)^n \end{aligned} \]

  问题即求 \(i\bmod4=0,1,2,3\) 时 \(a_i\) 倍 \([x^i]f(x)\) 之和。以 \(i\bmod4=0\) 为例:

\[\begin{aligned} \sum_{i=0}^n[4|i]a_0[x^i]f(x)&=\frac{1}4a_0\sum_{i=0}^n\left(\sum_{j=0}^3\omega_4^{ij}\right)\binom{n}is^i\\ &=\frac{1}4a_0\sum_{j=0}^3f(\omega_4^j) \end{aligned} \]

  直接代四个单位根进去算出来即可。对于其他三个 \(i\bmod4\) 的值,将 \(f\) 的各系数位移就能类似地求出答案。

  复杂度 \(\mathcal O(T\log n)\)(\(\times4^2\) 的常数)。

\(\mathcal{Code}\)

/* Clearink */

#include <cstdio>

#define rep( i, l, r ) for ( int i = l, repEnd##i = r; i <= repEnd##i; ++i )
#define per( i, r, l ) for ( int i = r, repEnd##i = l; i >= repEnd##i; --i )

typedef long long LL;

inline LL rint () {
	LL x = 0, f = 1; char s = getchar ();
	for ( ; s < '0' || '9' < s; s = getchar () ) f = s == '-' ? -f : f;
	for ( ; '0' <= s && s <= '9'; s = getchar () ) x = x * 10 + ( s ^ '0' );
	return x * f;
}

template<typename Tp>
inline void wint ( Tp x ) {
	if ( x < 0 ) putchar ( '-' ), x = -x;
	if ( 9 < x ) wint ( x / 10 );
	putchar ( x % 10 ^ '0' );
}

const int MOD = 998244353, G = 3, INV4 = 748683265;
LL n;
int w[4], s, a[4];

inline int mul ( const long long a, const int b ) { return a * b % MOD; }
inline int add ( int a, const int b ) { return ( a += b ) < MOD ? a : a - MOD; }
inline int mpow ( int a, int b ) {
	int ret = 1;
	for ( ; b; a = mul ( a, a ), b >>= 1 ) ret = mul ( ret, b & 1 ? a : 1 );
	return ret;
}

inline int f ( const int x ) {
	return mpow ( add ( mul ( s, x ), 1 ), n );
}

int main () {
	w[0] = 1, w[1] = mpow ( G, MOD - 1 >> 2 );
	w[2] = mul ( w[1], w[1] ), w[3] = mul ( w[2], w[1] );
	for ( int T = rint (); T--; ) {
		n = rint () % ( MOD - 1 ), s = rint ();
		rep ( i, 0, 3 ) a[i] = rint ();
		int ans = 0;
		rep ( r, 0, 3 ) {
			int res = 0;
			rep ( i, 0, 3 ) {
				res = add ( res,
					mul ( f ( w[i] ), mpow ( w[r * i & 3], MOD - 2 ) ) );
			}
			ans = add ( ans, mul ( res, a[r] ) );
		}
		wint ( mul ( ans, INV4 ) ), putchar ( '\n' );
	}
	return 0;
}

标签:LJJ,const,LOJ,sum,Solution,int,inline,mul,MOD
来源: https://www.cnblogs.com/rainybunny/p/14237899.html