Logo Wy Online Judge

WyOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#336#7. 「WyOJ Round 1」归 · 星穗垂野Pigsyy1007719ms340556kbC++233.0kb2025-04-18 20:58:502025-04-18 20:58:51

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;

int dp[1005][1005],pres[1005][1005];
int T,n,C,b[100005],ans;
signed flog[100005],a[100005],rmq[100005][17];
signed rot[100005],ls[40000005],rs[40000005],pt1;
int mnm[40000005];

char kudu;

int read(){
	int x=0,ch=1;for(;!isdigit(kudu);kudu=getchar())if(kudu=='-')ch=-1;
	for(;isdigit(kudu);kudu=getchar())x=x*10+(kudu^48);
	return x*ch;
}

int ql,qr,qv;

inline int gcd(int x,int y){
	while(y){int r=y;y=x%y;x=r;}
	return x;
}

inline int nmsl(int l,int r){
	int c=flog[r-l+1];return gcd(rmq[l][c],rmq[r-(1<<c)+1][c]);
}

inline int build(int l,int r){
	int x=++pt1;mnm[x]=0x3f3f3f3f3f3f3f3f;if(l==r)return x;
	int md=(l+r>>1);ls[x]=build(l,md);rs[x]=build(md+1,r);return x;
}

int add(int ox,int l,int r){
	int x=++pt1;mnm[x]=min(mnm[ox],qv);if(l==r)return x;
	int md=(l+r>>1);
	ls[x]=(ql<=md?add(ls[ox],l,md):ls[ox]);
	rs[x]=(ql>md?add(rs[ox],md+1,r):rs[ox]);
	return x;
}

void query(int x,int l,int r){
	if(ql>r||qr<l)return;if(ql<=l&&qr>=r){qv=min(qv,mnm[x]);return;}
	int md=(l+r>>1);query(ls[x],l,md);query(rs[x],md+1,r);
}

signed main(){
	for(int i=2;i<=100000;i++)flog[i]=flog[i>>1]+1;
	cin.tie(0)->sync_with_stdio(0);
	T=read();
	while(T--){
		n=read();C=read();
		for(int i=1;i<=n;i++){
			a[i]=read();rmq[i][0]=a[i];
		}
		for(int i=1;i<=n;i++){
			b[i]=read();b[i]+=b[i-1];
		}
		if(n<=10){
			ans=0x3f3f3f3f3f3f3f3f;
			for(int i=0;i<=n;i++)for(int j=0;j<=n;j++)dp[i][j]=0x3f3f3f3f3f3f3f3f;
			for(int i=0;i<=n;i++)for(int j=0;j<=n;j++)pres[i][j]=0x3f3f3f3f3f3f3f3f;
			dp[0][0]=0;for(int j=0;j<=n;j++)pres[0][j]=0;
			for(int i=1;i<=n;i++){
				for(int j=0;j<=n;j++)dp[i][j]=dp[i-1][j];
				for(int j=i,g=0;j>=1;j--){
					g=gcd(g,a[j]);
					if(g<=i-j+1)dp[i][i-j+1]=min(dp[i][i-j+1],pres[j-1][g]+g*(b[i]-b[j-1])+C);
				}
				pres[i][0]=dp[i][0];for(int j=1;j<=n;j++)pres[i][j]=min(pres[i][j-1],dp[i][j]);
			}
			for(int j=1;j<=n;j++)ans=min(ans,dp[n][j]);
			cout<<ans<<'\n';continue;
		}
		for(int o=1;o<17;o++){
			for(int i=1;i<=n-(1<<o)+1;i++)rmq[i][o]=gcd(rmq[i][o-1],rmq[i+(1<<o-1)][o-1]);
		}
		rot[0]=build(0,100000);
		ql=qv=0;rot[0]=add(rot[0],0,100000);
		for(int i=1;i<=n;i++){
			rot[i]=rot[i-1];
			for(int j=i,g=a[i],nmzl=0x3f3f3f3f3f3f3f3f;;){
				int lj=i;
				for(int o=16;o>=0;o--){
					if(lj>=(1<<o)&&rmq[lj-(1<<o)+1][o]%g==0)lj-=(1<<o);
				}
				if(g<=i-j+1){
					ql=0;qr=g;qv=0x3f3f3f3f3f3f3f3f;query(rot[j-1],0,100000);
					ql=i-j+1;qv+=C+g*(b[i]-b[j-1]);
					if(qv<nmzl){nmzl=qv;rot[i]=add(rot[i],0,100000);}
				}
				else if(g<=i-lj){
					ql=0;qr=g;qv=0x3f3f3f3f3f3f3f3f;query(rot[i-g],0,100000);
					ql=g;qv+=C+g*(b[i]-b[i-g]);
					if(qv<nmzl){nmzl=qv;rot[i]=add(rot[i],0,100000);}
				}
				if(lj){
					g=nmsl(lj,i);j=lj;
				}
				else break;
			}
		}
		ql=1;qr=100000;qv=0x3f3f3f3f3f3f3f3f;query(rot[n],0,100000);cout<<qv<<'\n';
		for(int i=1;i<=pt1;i++)ls[i]=rs[i]=0;pt1=0;
	}
	return 0;
}

这程序好像有点Bug,我给组数据试试?

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 5ms
memory: 8052kb

input:

1000
10 -759451753
8 10 5 7 5 10 6 10 2 1
5 6 5 4 9 8 5 4 1 3
10 -953033990
4 10 10 8 8 2 1 7 4 1
0 ...

output:

-2278355214
-2859101918
-21349075
-1203387136
-1290642033
-851981042
-648655823
-1699966984
-1493814...

result:

ok 1000 numbers

Test #2:

score: 10
Accepted
time: 410ms
memory: 49452kb

input:

5
94253 6998801
88374 94432 21568 79110 3746 94223 86214 12083 54743 1622 16110 18595 18994 3665 547...

output:

6999125
764376403
718240695
1381649585
885156744

result:

ok 5 number(s): "6999125 764376403 718240695 1381649585 885156744"

Test #3:

score: 10
Accepted
time: 12ms
memory: 16488kb

input:

5
120 -848255662
53358 95098 92852 36846 80052 83050 44071 22632 50005 52339 7339 38495 66618 6337 5...

output:

-11025209476
-3953017107
-5975377615
-2466586019
-5823678268

result:

ok 5 number(s): "-11025209476 -3953017107 -5975377615 -2466586019 -5823678268"

Test #4:

score: 10
Accepted
time: 7ms
memory: 15656kb

input:

5
100 -964452256
94167 36596 60051 82090 55625 56197 94589 29844 14929 68497 81714 90264 66283 96767...

output:

-7714370390
-19569375783
-554464800
-1059581333
-6827008162

result:

ok 5 number(s): "-7714370390 -19569375783 -554464800 -1059581333 -6827008162"

Test #5:

score: 10
Accepted
time: 27ms
memory: 11920kb

input:

5
984 -715414516
65632 15191 55392 53178 88695 20308 44882 16528 12986 81169 76880 2239 81264 13866 ...

output:

-72952473687
-64021627506
-700730085
-994178012
-1624804963

result:

ok 5 number(s): "-72952473687 -64021627506 -700730085 -994178012 -1624804963"

Test #6:

score: 10
Accepted
time: 1410ms
memory: 258324kb

input:

5
83593 -175988991
46605 26358 5204 30137 41225 11710 75224 14130 56433 62524 29873 59483 22318 2619...

output:

-1656361289384
-4920133073293
-1028097824
-2809116480
-22668175189

result:

ok 5 number(s): "-1656361289384 -4920133073293 ...097824 -2809116480 -22668175189"

Test #7:

score: 10
Accepted
time: 1617ms
memory: 340556kb

input:

5
96302 -451340376
2672 47391 49737 61648 38099 10679 28316 91168 97182 40556 20560 89630 45931 9187...

output:

-4877361987440
-9327808595024
-261498007
97842263
-37427524204

result:

ok 5 number(s): "-4877361987440 -9327808595024 -261498007 97842263 -37427524204"

Test #8:

score: 10
Accepted
time: 1619ms
memory: 327656kb

input:

5
82075 -915749780
41503 94470 21657 60519 10090 65640 80911 29436 63096 68715 79609 87499 26240 606...

output:

-8500967499879
-2162204721519
-803472133
-6968294203
-60694304098

result:

ok 5 number(s): "-8500967499879 -2162204721519 ...472133 -6968294203 -60694304098"

Test #9:

score: 10
Accepted
time: 1270ms
memory: 141296kb

input:

5
86206 -158351237
12910 53574 97622 75378 61116 40492 87106 60678 72614 2146 43673 15147 23123 7469...

output:

-1508037508585
-4714642449519
-3202809425
-6778545312
-12974512083

result:

ok 5 number(s): "-1508037508585 -4714642449519 ...809425 -6778545312 -12974512083"

Test #10:

score: 10
Accepted
time: 1342ms
memory: 207412kb

input:

5
87683 -236086252
88768 76878 54808 98282 70961 83599 48353 34381 40413 95288 24241 74500 52966 994...

output:

-2319782085288
-10990638538852
-1651187059
-12428157994
-22719490815

result:

ok 5 number(s): "-2319782085288 -10990638538852...87059 -12428157994 -22719490815"

Extra Test:

score: 0
Extra Test Passed