Logo Wy Online Judge

WyOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#348#7. 「WyOJ Round 1」归 · 星穗垂野WXZY30325ms52504kbC++232.2kb2025-04-20 17:33:312025-04-20 17:33:32

answer

#include<bits/stdc++.h>
using namespace std;
// #define int long long
#define ll long long
#define PII pair<int,int>
#define endl '\n'
#define lson 2*p
#define rson 2*p+1
const int N=1e5+10;
const int mod=1e9+7;
int f[N][20];
int a[N];
int n,c,tot;
ll w[N*17*18],sum[N];
int rt[N];
int ls[N*17*18],rs[N*17*18];
int lg[N],pw[N];
int gcd(int a,int b) {return (b?gcd(b,a%b):a);}
void init() {
	for(int i=1;i<=n;i++) f[i][0]=a[i];
	for(int j=1;j<=18;j++) {
		for(int i=1;i+pw[j]-1<=n;i++) {
			f[i][j]=gcd(f[i][j-1],f[i+(pw[j-1])][j-1]);
		}
	}
}
int query(int l,int r) {
	int k=lg[r-l+1];
	return gcd(f[l][k],f[r-pw[k]+1][k]);
}
void pushup(int now) {
	w[now]=min(w[ls[now]],w[rs[now]]);
}
int build(int l,int r)
{
	int u=++tot; w[tot]=1e18;
	if(l<r)
	{
		int mid=((l+r)>>1);
		ls[u]=build(l,mid),rs[u]=build(mid+1,r);
	}
	return u;
}
int modify(int now,int l,int r,int pos,ll x) {
	int v=++tot;
	w[v]=min(w[now],x);
	ls[v]=ls[now],rs[v]=rs[now];
	if(l<r) {
		int mid=l+r>>1;
		if(pos<=mid) ls[v]=modify(ls[v],l,mid,pos,x);
		else rs[v]=modify(rs[v],mid+1,r,pos,x);
	}
	return v;
}
ll query(int now,int l,int r,int ql,int qr) {
	if(l>=ql&&r<=qr) return w[now];
	ll ans=1e18;
	int mid=l+r>>1;
	if(ql<=mid) ans=min(ans,query(ls[now],l,mid,ql,qr));
	if(qr>mid) ans=min(ans,query(rs[now],mid+1,r,ql,qr));
	return ans;
}
void solve() {
	cin>>n>>c;
	for(int i=1;i<=n;i++) cin>>a[i],rt[i]=0;
	tot=0;
	build(0,n);
	rt[0]=0;
	rt[0]=modify(0,0,n,0,0);
	for(int i=1;i<=n;i++) {
		int x;
		cin>>x;
		sum[i]=sum[i-1]+x;
	}
	init();
	for(int i=1;i<=n;i++) {
		int len=1;
		rt[i]=rt[i-1];
		while(len<=i) {
			int g=query(i-len+1,i);
			int res=len,l=1,r=i;
			while(l<=r) {
				int mid=l+r>>1;
				if(query(i-mid+1,i)==g) res=mid,l=mid+1;
				else r=mid-1;
			}
			if(g<=res) {
				int j=max(g,len);
				ll val=query(rt[i-j],0,n,0,g)+g*(sum[i]-sum[i-j])+c;
				rt[i]=modify(rt[i],0,n,j,val);
			}
			len=res+1;
		}
	}
	cout<<query(rt[n],0,n,1,n)<<"\n";
}
signed main() {
    int T=1;
    ios::sync_with_stdio(false);
    cin.tie(0);
	cout.tie(0);
	lg[0]=-1;
	for(int i=1;i<=1e5;i++) lg[i]=lg[i/2]+1;
	pw[0]=1;
	for(int i=1;i<=20;i++) pw[i]=pw[i-1]*2;
    cin>>T;
    while(T--) {    
        solve();
    }
}

详细

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

Test #1:

score: 0
Wrong Answer
time: 9ms
memory: 12032kb

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:

wrong answer 990th numbers differ - expected: '321293010', found: '0'

Test #2:

score: 0
Wrong Answer
time: 291ms
memory: 52504kb

input:

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

output:

0
0
0
0
0

result:

wrong answer 1st numbers differ - expected: '6999125', found: '0'

Test #3:

score: 10
Accepted
time: 3ms
memory: 12152kb

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: 5ms
memory: 9940kb

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: 17ms
memory: 14240kb

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: 0
Runtime Error

input:

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

output:


result:


Test #7:

score: 0
Runtime Error

input:

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

output:


result:


Test #8:

score: 0
Runtime Error

input:

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

output:


result:


Test #9:

score: 0
Runtime Error

input:

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

output:


result:


Test #10:

score: 0
Runtime Error

input:

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

output:


result: