HDU5829 NTT
以下这份代码并没有过。但感觉没有问题。不是蜜汁WA就是蜜汁T。
1 #include <cstdio> 2 #include <iostream> 3 #include <cstring> 4 #include <algorithm> 5 typedef long long ll; 6 using namespace std; 7 const int mod = 998244353; 8 const int N = 262144, K = 17; 9 int i; 10 int P = 998244353, G = 3, g[K+1], ng[K+10], inv[N+10]; 11 ll A[N*2+10], B[N*2+10]; 12 ll pow(ll a, int n){ 13 ll ans = 1; 14 while(n){ 15 if(n&1) 16 ans = ans*a%P; 17 a = a*a%P; 18 n >>= 1; 19 } 20 return ans; 21 } 22 void NTT(ll*a,int n,int t){ 23 for(int i=1,j=0;i<n-1;i++){ 24 for(int s=n;j^=s>>=1,~j&s;); 25 if(i<j)swap(a[i], a[j]); 26 } 27 for(int d=0;(1<<d)<n;d++){ 28 int m=1<<d,m2=m<<1; 29 ll _w=pow(G, (P-1)>>(d+1)); 30 if(t != 1) _w = pow(_w, P-2); 31 for(int i=0;i<n;i+=m2)for(ll w=1,j=0;j<m;j++){ 32 ll&A=a[i+j+m],&B=a[i+j],t=w*A%P; 33 A=B-t;if(A<0)A+=P; 34 B=B+t;if(B>=P)B-=P; 35 w=w*_w%P;////////////////////// 36 } 37 } 38 if(t==-1)for(int i=0,j=pow(n, P-2);i<n;i++)a[i]=a[i]*j%P; 39 } 40 41 /*****************另一份NTT模板 42 void change (ll y[], int len) { 43 for(int i = 1, j = len / 2; i < len - 1; i++) { 44 if(i < j) swap(y[i], y[j]); 45 int k = len / 2; 46 while(j >= k) { 47 j -= k; 48 k /= 2; 49 } 50 if(j < k) j += k; 51 } 52 } 53 void ntt(ll y[], int len, int on) { 54 change (y, len); 55 for(int h = 2; h <= len; h <<= 1) { 56 ll wn = pow(G, (mod-1)/h); 57 if(on == -1) wn = pow(wn, mod-2); 58 for(int j = 0; j < len; j += h) { 59 long long w = 1; 60 for(int k = j; k < j + h / 2; k++) { 61 long long u = y[k]; 62 long long t = w * y[k + h / 2] % mod; 63 y[k] = (u + t) % mod; 64 y[k+h/2] = (u - t + mod) % mod; 65 w = w * wn % mod; 66 } 67 } 68 } 69 if(on == -1) { 70 long long t = pow (len, mod - 2); 71 for(int i = 0; i < len; i++) 72 y[i] = y[i] * t % mod; 73 } 74 } 75 ***************************/ 76 77 int a[N]; 78 int pow2[N], r[N], f[N], F[N]; 79 80 int main(){ 81 // freopen("in", "r", stdin); 82 // freopen("outd", "w", stdout); 83 // for(g[K]=pow(G,(P-1)/N),ng[K]=pow(g[K],P-2),i=K-1;~i;i--) 84 // g[i]=(ll)g[i+1]*g[i+1]%P, ng[i]=(ll)ng[i+1]*ng[i+1]%P; 85 for(inv[1]=inv[0]=1,i=2;i<N;i++) inv[i]=(ll)(P-inv[P%i])*(P/i)%P; 86 87 for(f[0]=f[1]=r[0]=r[1]=pow2[0]=1, i=pow2[1]=2; i < N; i++){ 88 r[i] = (ll)inv[i]*r[i-1]%P; 89 f[i] = (ll)i*f[i-1]%P; 90 pow2[i] = (pow2[i-1]<<1)%P; 91 } 92 93 int t, ca = 1, n; scanf("%d", &t); 94 while(t--){ 95 scanf("%d", &n); 96 for(i = 1; i <= n; i++) scanf("%d", a+i); 97 sort(a+1, a+n+1, greater<int>() ); 98 99 memset(B, 0, sizeof(B)); 100 memset(A, 0, sizeof(A)); 101 for(i = 1; i <= n; i++){ 102 B[i-1] = (ll)f[i-1]*pow2[n-i]%P*a[i]%P; 103 A[i-1] = r[n-i]; 104 } 105 106 int len = 1; 107 while(len <= n+n) len <<= 1; 108 NTT(B, len, 1), NTT(A, len, 1); 109 for(int i = 0; i < len; i++) 110 A[i] = B[i]*A[i]%P; 111 NTT(A, len, -1); 112 113 for(int i = 1; i <= n; i++){ 114 F[i] = A[n-2+i]*(ll)r[i-1]%P+F[i-1]; 115 if(F[i] >= P) F[i] %= P; 116 printf("%d ", F[i]); 117 } 118 puts(""); 119 } 120 return 0; 121 }
以上代码蜜汁T,NTT()换成ntt()后蜜汁WA。
和AC代码对拍很久也没看出什么问题。
诸神对凡人心生艳羡,厌倦天堂。