QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#710486#9526. Subsequence Counting275307894aTL 17ms9544kbC++143.1kb2024-11-04 20:03:582024-11-04 20:03:59

Judging History

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

  • [2024-11-04 20:03:59]
  • 评测
  • 测评结果:TL
  • 用时:17ms
  • 内存:9544kb
  • [2024-11-04 20:03:58]
  • 提交

answer

#include<bits/stdc++.h>
#define Gc() getchar()
#define Me(x,y) memset(x,y,sizeof(x))
#define Mc(x,y) memcpy(x,y,sizeof(x))
#define d(x,y) ((m)*(x-1)+(y))
#define R(n) (rnd()%(n)+1)
#define Pc(x) putchar(x)
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define eb emplace_back
#define all(x) x.begin(),x.end()
using namespace std;using ll=long long;using db=double;using lb=long db;using ui=unsigned;using ull=unsigned long long;using pii=pair<int,int>;
const int N=3e3+5,M=300+5,K=1000+5,mod=998244353,Mod=mod-1;const db eps=1e-9;const int INF=1e9+7;mt19937 rnd(28382);
#define Tp template<typename T>
#define Ts template<typename T,typename... Ar>
namespace Debug{
	Tp void _debug(char* f,T t){cerr<<f<<'='<<t<<endl;}
	Ts void _debug(char* f,T x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);}
	#ifdef LOCAL
	#define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__)
	#else 
	#define gdb(...) void()
	#endif
}using namespace Debug;
int n,m,k,L;
int B[N];
void exgcd(int x,int y,int &a,int &b){
	if(!y){a=1;b=0;return;}
	exgcd(y,x%y,b,a);b-=x/y*a;
}
ll inve(int x,int y){
	int a,b;exgcd(x,y,a,b);
	return (a%y+y)%y;
}
using mat=array<array<ull,11>,11>;
mat I,Z;
mat operator *(const mat &A,const mat &B){
	mat C=Z;
	for(int x=0;x<=m;x++) for(int y=x;y<=m;y++) for(int z=y;z<=m;z++) C[x][z]+=A[x][y]*B[y][z];
	for(int x=0;x<=m;x++) for(int y=x;y<=m;y++) C[x][y]%=mod;
	return C;
}
using info=vector<pair<int,mat> >;
mat mpow(mat x,int y){mat ans=I;while(y) y&1&&(ans=ans*x,0),y>>=1,x=x*x;return ans;}
namespace DS{
	mat f[N];
	void build(){
		for(int i=0;i<n+100;i++) f[i]=I;
	}
	void modify(int x,mat y){
		f[x]=y;
	}
	mat qry(){
		mat ans=I;
		for(int i=0;i<n+100;i++) ans=ans*f[i];
		return ans;
	}
}
void calc(int L,int k,info A){
	gdb(L,k,A.size());
	if(L==1){
		for(int x=0;x<=m;x++) for(int y=x;y<=m;y++) gdb(x,y,A[0].se[x][y]);
		printf("%lld\n",A[0].se[0][m]);
		return;
	}
	k%=L;
	if(2*k>L){
		reverse(A.begin()+1,A.end());
		if(A[0].fi^1) A.emplace_back(A[0].fi-1,A[0].se),A[0].fi=1; 
		k=L-k;
	}
	int x=0;
	vector<pii> B;
	DS::build();
	static int cnt[N];
	for(int i=0;i<A.size();i++) cnt[i]=0;
	auto add=[&](int x,int w){
		cnt[x]+=w;
		DS::modify(x,mpow(A[x].se,cnt[x]));
	};
	for(int i=0;i<A.size();i++){
		gdb(A[i].fi);
		add(i,(x+A[i].fi+k-1)/k-(x+k-1)/k);
		x+=A[i].fi;
		if(x%k) B.emplace_back(x%k,i);
	}
	sort(all(B));
	info C;
	C.emplace_back(B.empty()?k:B[0].fi,DS::qry());
	for(int i=0;i<B.size();i++){
		add(B[i].se,-1);
		if(B[i].se+1<A.size()) add(B[i].se+1,1);
		int r=(i+1==B.size()?k:B[i+1].fi);
		gdb(B[i].fi,r);
		if(B[i].fi^r) C.emplace_back(r-B[i].fi,DS::qry());
	}
	calc(k,k-L%k,C);
}
void Solve(){
	scanf("%d%d%d%d",&n,&m,&k,&L);
	k=inve(k,L);
	for(int i=0;i<=m;i++) I[i][i]=1;
	for(int i=1;i<=m;i++) scanf("%d",&B[i]);
	info A(n);
	for(int i=0;i<n;i++){
		int x,y;scanf("%d%d",&x,&y);
		A[i]=make_pair(x,I);
		for(int j=1;j<=m;j++) if(y==B[j]) A[i].se[j-1][j]=1;
	}
	calc(L,k,A);
}
int main(){
	int t=1;
	// scanf("%d",&t);
	while(t--) Solve();
	cerr<<clock()*1.0/CLOCKS_PER_SEC<<'\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 4100kb

input:

4 2 17 27
3 1
10 3
6 1
10 3
1 1

output:

76

result:

ok single line: '76'

Test #2:

score: 0
Accepted
time: 1ms
memory: 4148kb

input:

5 3 1789 15150
555 718 726
72 555
1029 718
5807 726
1002 718
7240 555

output:

390415327

result:

ok single line: '390415327'

Test #3:

score: 0
Accepted
time: 0ms
memory: 4076kb

input:

1 1 1 1000000000
1000
1000000000 1000

output:

1755647

result:

ok single line: '1755647'

Test #4:

score: 0
Accepted
time: 17ms
memory: 9544kb

input:

1999 10 999999999 1000000000
944 901 986 915 979 988 947 999 969 946
198832 985
235662 982
367137 938
219700 949
166086 906
488084 905
891250 984
243743 971
253382 987
181971 935
2382 948
462701 981
4681 925
113363 916
119397 921
337742 982
427128 921
285959 986
197975 978
140753 907
167150 974
4576...

output:

211590728

result:

ok single line: '211590728'

Test #5:

score: -100
Time Limit Exceeded

input:

2000 10 207560381 499173270
382 246 825 688 810 66 333 717 903 444
242322 825
303194 246
266460 66
133293 444
499376 688
175256 333
260041 717
202138 444
84931 688
353443 825
137750 382
333307 66
450617 810
27484 246
349436 717
45179 444
146073 810
107846 717
416816 246
255378 825
433826 688
273215 ...

output:


result: