QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#710485#9526. Subsequence Counting275307894aRE 1ms4228kbC++143.1kb2024-11-04 20:03:422024-11-04 20:03:42

Judging History

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

  • [2024-11-04 20:03:42]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:4228kb
  • [2024-11-04 20:03:42]
  • 提交

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=2000+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: 0ms
memory: 4056kb

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: 4228kb

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: 3936kb

input:

1 1 1 1000000000
1000
1000000000 1000

output:

1755647

result:

ok single line: '1755647'

Test #4:

score: -100
Runtime Error

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:


result: