QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#854782#7561. Digit DPinksamuraiAC ✓2294ms511752kbC++235.3kb2025-01-12 08:09:312025-01-12 08:09:31

Judging History

This is the latest submission verdict.

  • [2025-01-12 08:09:31]
  • Judged
  • Verdict: AC
  • Time: 2294ms
  • Memory: 511752kb
  • [2025-01-12 08:09:31]
  • Submitted

answer

#include <bits/stdc++.h>
#define int ll
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define rng(i,c,n) for(int i=c;i<n;i++)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
#define vec(...) vector<__VA_ARGS__>
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}

typedef __int128 Int;

// snuke's mod int
template <ll mod>
struct modint{
	ll x;
	modint(ll x=0):x((x%mod+mod)%mod){}
	modint operator-()const{return modint(-x);}
	modint& operator+=(const modint a){if((x+=a.x)>=mod) x-=mod; return *this;}
	modint& operator-=(const modint a){if((x+=mod-a.x)>=mod) x-=mod; return *this;}
	modint& operator*=(const modint a){(x*=a.x)%=mod; return *this;}
	modint operator+(const modint a)const{modint res(*this); return res+=a;}
	modint operator-(const modint a)const{modint res(*this); return res-=a;}
	modint operator*(const modint a)const{modint res(*this); return res*=a;}
	modint pow(ll n)const{
		modint res=1,x(*this);
		while(n){
			if(n&1)res*=x;
			x*=x;
			n>>=1;
		}
		return res;
	}
	modint inv()const{return pow(mod-2);}
};

using mint=modint<998244353>;

typedef vec(mint) vm;

struct F{
	mint len=0;
	vm dp={0,0,0};
	F operator+(const F&a)const{
		F res;
		rep(j,3) res.dp[j]+=a.dp[j];
		return res;
	}
	void add(mint x){
		dp[2]=dp[2]+dp[1]*(len-2)*x*mint(3)+dp[0]*(len-1)*(len-2)*x*x*mint(3)+x*x*x*len*(len-1)*(len-2);
		dp[1]=dp[1]+dp[0]*(len-1)*x*mint(2)+x*x*len*(len-1);
		dp[0]=dp[0]+x*len;
	}
};

F merge(F l,F r){
	F res;
	res.len=l.len+r.len;
	res.dp[0]=l.dp[0]+r.dp[0];
	res.dp[1]+=l.dp[1];
	res.dp[1]+=r.dp[1];
	res.dp[1]+=(l.dp[0]*r.dp[0])*mint(2);
	res.dp[2]+=l.dp[2];
	res.dp[2]+=r.dp[2];
	res.dp[2]+=l.dp[0]*r.dp[1]*mint(3);
	res.dp[2]+=l.dp[1]*r.dp[0]*mint(3);
	return res;
}

void print(Int n){
	if(n==0) return;
	print(n/10);
	cout<<(int)(n%10);
}

int n,q;

vi a;

vec(F) ps;

Int pot[210];

int ptr=0;

struct N{
	int sart=0;
	int lc=-1,rc=-1;
	int tag=0;
	mint ad=0;
	F res;
}seg[5000000];

void apply(int node){
	if(seg[node].sart!=n and seg[node].lc==-1){
		seg[node].lc=++ptr;
		seg[ptr].sart=seg[node].sart+1;
		// print(seg[ptr].sart);
		if(n-seg[ptr].sart<0){
			print("wtf");
			exit(0);
		}
		seg[ptr].res=ps[n-seg[ptr].sart];
	}
	if(seg[node].sart!=n and seg[node].rc==-1){
		seg[node].rc=++ptr;
		seg[ptr].sart=seg[node].sart+1;
		// print(seg[ptr].sart);
		if(n-seg[ptr].sart<0){
			print("wtf");
			exit(0);
		}
		seg[ptr].res=ps[n-seg[ptr].sart];
	}
	if(seg[node].tag){
		mint x=seg[node].ad;
		seg[node].res.add(x);
		if(seg[node].lc!=-1){
			seg[seg[node].lc].tag=1;
			seg[seg[node].lc].ad+=x;
		}
		if(seg[node].rc!=-1){
			seg[seg[node].rc].tag=1;
			seg[seg[node].rc].ad+=x;
		}
		seg[node].tag=0;
		seg[node].ad=0;
	}
}

F prod(int node,Int l,Int r,Int s,Int e){
	apply(node);
	if(l>=e or r<=s){
		return F{};
	}
	// print(l);
	// print();
	// print(r);
	// print();
	if(s<=l and r<=e){
		// print("wtf go",node,n-seg[node].sart);
		// print(l);
		// print();
		// print(r);
		// print();
		// print((int)l,(int)r,(int)(s),(int)(e));
		return seg[node].res;
	}
	Int m=(l+r)/2;
	// print("wtf");
	// print(s);
	// print();
	// print(e);
	// print();
	// return F{};
	auto val_l=prod(seg[node].lc,l,m,s,e);
	auto val_r=prod(seg[node].rc,m,r,s,e);
	if(seg[node].sart!=n) val_r.add(a[n-1-seg[node].sart]);
	// print((int)l,(int)r,(int)(s),(int)(e));
	// print(val_r.len.x,a[n-1-seg[node].sart]);
	return merge(val_l,val_r);
	// return merge(prod(seg[node].lc,l,m,s,e),);
}

void add(int node,Int l,Int r,Int s,Int e,int x){
	apply(node);
	if(l>=e or r<=s){
		return;
	}
	if(s<=l and r<=e){
		seg[node].tag=1;
		seg[node].ad+=x;
		apply(node);
		return;
	}
	Int m=(l+r)/2;
	add(seg[node].lc,l,m,s,e,x);
	add(seg[node].rc,m,r,s,e,x);
	auto val_l=seg[seg[node].lc].res;
	auto val_r=seg[seg[node].rc].res;
	if(seg[node].sart!=n) val_r.add(a[n-1-seg[node].sart]);
	// print((int)l,(int)r,(int)(s),(int)(e));
	// print(val_r.len.x,a[n-1-seg[node].sart]);
	seg[node].res=merge(val_l,val_r);
}

Int toInt(string s){
	Int now=0;
	rep(i,sz(s)){
		now*=2;
		now+=(s[i]-'0');
	}
	return now;
}

void slv(){
	cin>>n>>q;

	a=vi(n,0);
	rep(i,n) cin>>a[i];

	F now;
	now.len=1;
	ps.pb(now);
	rep(i,n){
		now=ps.back();
		now.add(a[i]);
		// print(now.dp[2].x);
		ps.pb(merge(ps.back(),now));
	}

	pot[0]=1;
	rng(i,1,n+1) pot[i]=pot[i-1]*Int(2);

	{
		int node=0;
		seg[node].sart=0;
		seg[node].res=ps[n];
		seg[node].res.len=pot[n];
	}

	rep(i,q){
		int t;
		cin>>t;
		string l,r;
		cin>>l>>r;
		{
			bool fnd=0;
			per(j,sz(r)){
				if(r[j]=='0'){
					fnd=1;
					r[j]='1';
					break;
				}
				r[j]='0';
			}
			if(!fnd) r.insert(r.begin(),'1');
		}
		// print(r);
		if(t==1){
			int x;
			cin>>x;
			add(0,0,pot[n],toInt(l),toInt(r),x);
		}else{
			// print(toInt(l));
			// print(toInt(r));
			// print("e");
			auto res=prod(0,0,pot[n],toInt(l),toInt(r));
			mint ans=res.dp[2];
			ans*=mint(6).inv();
			print(ans.x);
		}
	}
}

signed main(){
	ios::sync_with_stdio(0),cin.tie(0);
	slv();
}

詳細信息

Test #1:

score: 100
Accepted
time: 148ms
memory: 511396kb

input:

3 3
1 2 4
2 000 111
1 010 101 1
2 000 111

output:

1960 
3040 

result:

ok 2 number(s): "1960 3040"

Test #2:

score: 0
Accepted
time: 194ms
memory: 511464kb

input:

2 2
1 1
2 00 10
2 00 11

output:

0 
2 

result:

ok 2 number(s): "0 2"

Test #3:

score: 0
Accepted
time: 2210ms
memory: 511448kb

input:

99 49952
470888 74578 802746 396295 386884 721198 628655 722503 207868 647942 87506 792718 761498 917727 843338 908043 952768 268783 375312 414369 319712 96230 277106 168102 263554 936674 246545 667941 198849 268921 191459 436316 134606 802932 515506 837311 465964 394766 17626 650419 984050 790137 4...

output:

413449847 
513027341 
379532803 
828185770 
758023792 
955841012 
501590435 
193833160 
52015005 
225646848 
79278417 
702315659 
500712121 
309545833 
425668668 
376205546 
751940860 
216608361 
71293362 
894788412 
240680508 
127400767 
584610664 
427310971 
447134022 
992309654 
109125715 
611523...

result:

ok 24939 numbers

Test #4:

score: 0
Accepted
time: 2176ms
memory: 511488kb

input:

99 49996
475634 928248 927808 875072 158867 937890 595515 26685 468307 240390 887597 586447 764525 365644 156469 188306 350786 308832 695957 562147 427221 937909 590963 478310 357775 361535 993561 967811 718075 555000 533750 412453 936715 173340 350235 67386 20497 895277 233727 235830 182535 324591 ...

output:

259953307 
262069796 
924406695 
26478563 
298108385 
704809872 
792095151 
692313907 
142605670 
903738194 
553847857 
38647574 
43984176 
29033158 
867129569 
773316503 
446446137 
689917105 
416630991 
420951134 
458731790 
810982529 
271786324 
784672540 
32086643 
884115047 
362416513 
75927993...

result:

ok 24966 numbers

Test #5:

score: 0
Accepted
time: 2179ms
memory: 511448kb

input:

97 49937
288891 590429 244358 321145 930851 89174 529670 363571 728747 543238 720391 188689 702144 813561 628383 660056 781508 605777 759705 485733 534730 812292 937524 788519 451996 10588 483682 267682 461493 65270 619145 355885 963015 800644 217669 264757 640439 685387 674020 853944 91420 891750 5...

output:

965092014 
894220805 
25147419 
773946359 
121175554 
920857234 
690801029 
201407028 
945021685 
635573900 
216040077 
104774110 
355850561 
561273301 
926775110 
372974907 
597614504 
942178785 
379372329 
754110414 
735461091 
710022471 
323330013 
717895783 
482511705 
946821704 
625188740 
2999...

result:

ok 25768 numbers

Test #6:

score: 0
Accepted
time: 2242ms
memory: 511456kb

input:

96 49981
102149 219907 593611 24114 959730 305867 496529 635050 21890 102981 487777 982418 896659 518374 876106 907614 179526 645826 856158 633510 642240 653971 475573 98727 513513 435449 165290 567552 980720 351348 994140 332021 797828 138348 52399 751728 227676 475498 922825 215163 289905 426204 7...

output:

274224174 
634217068 
813069780 
582646554 
692811965 
500277373 
820650745 
249911179 
910599837 
79752646 
454211240 
542480599 
531528915 
576664734 
417008251 
248368338 
924557955 
675037065 
933004411 
320044817 
134377085 
177982136 
923478201 
167853704 
738499226 
732464690 
723323846 
6614...

result:

ok 24458 numbers

Test #7:

score: 0
Accepted
time: 2181ms
memory: 511448kb

input:

99 49921
106895 882089 718673 502890 699009 489855 430685 939232 282330 630021 287868 584659 866982 966291 348020 379364 642952 942770 919906 781288 492853 752547 789430 217447 607734 893014 655411 867422 467242 828915 303728 275454 599937 732948 887129 981803 814914 8713 363118 833277 488390 960658...

output:

571914589 
935084376 
827788412 
707727385 
222848822 
789988142 
130081973 
890052791 
21823459 
198217451 
775618413 
943091375 
261240034 
711259481 
243909220 
167600347 
186737627 
526251657 
226935286 
979557550 
784330590 
857111244 
108590275 
746670632 
67900274 
551981921 
855980494 
24694...

result:

ok 24928 numbers

Test #8:

score: 0
Accepted
time: 2220ms
memory: 511752kb

input:

99 49970
887448 703054 67926 981667 695184 641139 364840 276118 318577 222469 896470 378388 28793 414208 595743 659626 40970 207011 207847 704874 600362 594226 168695 527655 701955 509363 369723 134588 210660 147697 613315 251590 434750 103356 721858 179173 402151 798823 546514 451392 654171 752009 ...

output:

994539861 
230160518 
831071911 
658104212 
646333204 
48758132 
438924579 
479652249 
500155431 
388305435 
61288261 
662022245 
836922136 
428322715 
754372301 
55811268 
812913663 
248594306 
932725310 
243841330 
342441725 
888780076 
877471721 
958979518 
295016896 
997768920 
253078043 
484841...

result:

ok 24970 numbers

Test #9:

score: 0
Accepted
time: 2189ms
memory: 511548kb

input:

97 49922
924898 332532 192988 684636 499872 857831 331700 547597 579017 525316 696560 204822 31820 862125 908873 131377 438988 312468 271596 852652 740575 501313 482552 837864 796176 934224 84035 210267 729886 657968 731414 195022 461051 697957 589292 409248 989389 523526 19511 812610 595760 286463 ...

output:

670642273 
100974501 
625973766 
105095407 
972918641 
230643745 
884360909 
863808877 
784806784 
361515233 
226518536 
681307050 
91526349 
382996995 
458256474 
766680719 
175217744 
990501348 
775220693 
121647158 
443490504 
964608278 
366850818 
295051421 
82689337 
499548119 
737432899 
47710...

result:

ok 25732 numbers

Test #10:

score: 0
Accepted
time: 2294ms
memory: 511452kb

input:

100 49963
705451 994713 509537 130709 463343 41819 265855 851779 839457 85060 496650 774359 193631 310042 380788 411639 869710 576709 368048 33133 623893 375696 796409 923880 114590 391789 574156 510137 249112 135534 41001 171159 263159 35661 391318 639323 576627 89445 235612 430725 794245 820918 89...

output:

915810899 
506294427 
47800009 
103639896 
956906949 
548330581 
732270643 
752575162 
498382746 
898706792 
533368210 
715772880 
170169296 
821669776 
366622196 
930058524 
422553215 
727535836 
456033290 
178329746 
702822832 
431557772 
991450571 
994720884 
841765419 
749599756 
642382643 
5780...

result:

ok 25238 numbers

Test #11:

score: 0
Accepted
time: 2220ms
memory: 511448kb

input:

99 49907
710197 624191 858791 609486 268030 225807 200011 188665 132600 612100 329445 633496 196658 757959 628510 883389 267729 840950 655989 180911 731402 217375 142970 299496 208811 8138 288468 810007 992530 421612 383292 81887 97972 662965 258752 836694 196568 846851 675905 791943 960026 388076 5...

output:

26550913 
37967518 
866129092 
449447729 
784627573 
957609030 
223734858 
109702716 
706620813 
908609246 
200088075 
65731593 
746451302 
791934803 
545413883 
279170991 
984659992 
16766707 
572424663 
67271436 
659220473 
679665329 
511747582 
303939869 
586840465 
557851982 
314773019 
66127766...

result:

ok 24947 numbers

Test #12:

score: 0
Accepted
time: 2235ms
memory: 511444kb

input:

98 49918
274071 359971 550121 204862 843967 173607 619138 690754 219513 171337 183499 549873 542337 661387 397647 495917 413076 918417 868000 422012 195703 305826 526356 334728 535984 133227 226371 632864 493387 611196 258251 576565 244054 713672 267148 679390 700005 67050 546349 2772 999375 951131 ...

output:

64412198 
438330476 
544087922 
183377287 
218050658 
63409640 
622983373 
792175595 
56162202 
109909817 
597953619 
475435831 
503222938 
971092944 
703746139 
370972476 
721890059 
256607366 
980618411 
389408168 
185601217 
807652101 
254330391 
642979159 
235228431 
627504981 
383641079 
760270...

result:

ok 25130 numbers