QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#820505#4411. Equipment UpgradeSGColinAC ✓776ms6652kbC++202.5kb2024-12-18 21:39:022024-12-18 21:39:04

Judging History

你现在查看的是最新测评结果

  • [2024-12-18 21:39:04]
  • 评测
  • 测评结果:AC
  • 用时:776ms
  • 内存:6652kb
  • [2024-12-18 21:39:02]
  • 提交

answer

#include<cstdio>
#include<cctype>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=100000,maxt=1<<18,MOD=998244353,INV100=828542813;

int te,n,p[maxn+5],c[maxn+5],w[maxn+5],sum[maxn+5],ans[maxn+5];
int wn[maxt+5],f[maxt+5],g[maxt+5];

#define EOLN(x) ((x)==10 || (x)==13 || (x)==EOF)
inline char readc(){
	static char buf[100000],*l=buf,*r=buf;
	return l==r && (r=(l=buf)+fread(buf,1,100000,stdin),l==r)?EOF:*l++;
}
template<typename T> int readi(T &x){
	T tot=0;char ch=readc(),lst='+';
	while (!isdigit(ch)) {if (ch==EOF) return EOF;lst=ch;ch=readc();}
	while (isdigit(ch)) tot=(tot<<3)+(tot<<1)+(ch^48),ch=readc();
	lst=='-'?x=-tot:x=tot;return EOLN(ch);
}
inline int ADD(int x,int y) {return x+y>=MOD?x+y-MOD:x+y;}
inline int MUL(int x,int y) {return (LL)x*y%MOD;}
int Pow(int w,int b) {int s;for (s=1;b;b>>=1,w=MUL(w,w)) if (b&1) s=MUL(s,w);return s;}
void NTTPre(){
	int x=Pow(3,(MOD-1)/maxt);
	wn[maxt/2]=1;
	for (int i=maxt/2+1;i<maxt;i++) wn[i]=MUL(wn[i-1],x);
	for (int i=maxt/2-1;i>=0;i--) wn[i]=wn[i<<1];
}
void NTT(int *a,int n,int f){
	if (f>0){
		for (int k=n>>1;k;k>>=1)
			for (int i=0;i<n;i+=k<<1)
				for (int j=0;j<k;j++){
					int x=a[i+j],y=a[i+j+k];
					a[i+j+k]=MUL(x+MOD-y,wn[k+j]);
					a[i+j]=ADD(x,y);
				}
	} else {
		for (int k=1;k<n;k<<=1)
			for (int i=0;i<n;i+=k<<1)
				for (int j=0;j<k;j++){
					int x=a[i+j],y=MUL(a[i+j+k],wn[k+j]);
					a[i+j+k]=ADD(x,MOD-y);
					a[i+j]=ADD(a[i+j],y);
				}
		for (int i=0,INV=MOD-(MOD-1)/n;i<n;i++) a[i]=MUL(a[i],INV);
		reverse(a+1,a+n);
	}
}
void Solve(int L,int R){
	if (L==R){
		if (L==1) {ans[L]=ADD(ans[L-1],c[L-1]);return;}
		ans[L]=MUL(ans[L],p[L-1]+MOD-1);
		ans[L]=MUL(ans[L],Pow(sum[L-1],MOD-2));
		ans[L]=ADD(ans[L],ADD(ans[L-1],c[L-1]));
		ans[L]=MUL(ans[L],Pow(p[L-1],MOD-2));
		return;
	}
	int mid=L+(R-L>>1);
	Solve(L,mid);
	int t;for (t=1;t<=R-L-1+mid-L;t<<=1);
	for (int i=0;i<=mid-L;i++) f[i]=ans[i+L];
	for (int i=mid-L+1;i<t;i++) f[i]=0;
	for (int i=0;i<=R-L-1;i++) g[i]=w[i];
	for (int i=R-L;i<t;i++) g[i]=0;
	NTT(f,t,1);NTT(g,t,1);
	for (int i=0;i<t;i++) f[i]=MUL(f[i],g[i]);
	NTT(f,t,-1);
	for (int i=mid-L;i<=R-L-1;i++) ans[i+L+1]=ADD(ans[i+L+1],f[i]);
	Solve(mid+1,R);
}
int main(){
	NTTPre();
	for (readi(te);te;te--){
		readi(n);
		for (int i=0;i<n;i++) readi(p[i]),p[i]=MUL(p[i],INV100),readi(c[i]);
		for (int i=1;i<n;i++) readi(w[i]),sum[i]=ADD(sum[i-1],w[i]);
		for (int i=0;i<=n;i++) ans[i]=0;
		Solve(1,n);
		printf("%d\n",ans[n]);
	}
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 776ms
memory: 6652kb

input:

208
2
100 41
28 64
28
3
100 48
91 13
73 3
78 92
4
100 22
15 85
26 50
41 15
90 85 77
5
100 39
51 97
83 41
4 86
36 70
49 24 17 33
6
100 53
53 45
92 2
36 40
61 61
76 52
18 37 75 49 96
7
100 5
21 47
39 58
78 1
82 93
59 82
56 90
1 41 76 64 84 27
8
100 14
38 77
66 20
1 63
47 47
3 12
87 16
99 62
14 81 75 2...

output:

375
243619761
141260443
516768753
850749960
897481401
602765935
510391586
689398435
784190268
697129546
505176530
687991734
16121189
684750916
616413796
324645467
60836964
997265902
829124402
135215114
115586183
566051860
45973142
577302112
438599189
808712026
903587073
180745041
931933480
429669755...

result:

ok 208 lines