1. 程式人生 > 其它 >2021.10.10考試總結[NOIP模擬73]

2021.10.10考試總結[NOIP模擬73]

T1小L的疑惑 T2小L的數列 T3連邊 T4小L的有向圖

T1 小L的疑惑

對於\(P_i\),如果所有比\(P_i\)小的數加起來也達不到\(P_i-1\),那麼值域肯定不連續。否則設原來值域最大值為\(mx\),則\(P_i\)會讓值域最大值增致\(mx+P_i\)

排序後掃一遍。

\(code:\)

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

namespace IO{
	auto read=[]()->int{
		char ch=getchar(); int x=0,f=1;
		while(ch<'0'||ch>'9'){ if(ch=='-') f=-1; ch=getchar(); }
		while(ch>='0'&&ch<='9'){ x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); }
		return x*f;
	};
	auto write=[](int x,int sp)->void{
		char ch[20]; int len=0;
		if(x<0){ x=~x+1; putchar('-'); }
		do{ ch[len++]=(1<<4)+(1<<5)+x%10; x/=10; }while(x);
		for(int i=len-1;~i;--i) putchar(ch[i]); putchar(sp);
	};
	auto ckmax=[](int& x,int y)->void{ x=x<y?y:x; };
	auto ckmin=[](int& x,int y)->void{ x=x<y?x:y; };
} using namespace IO;

const int NN=100010;
int n,p[NN],f[NN];

signed main(){
	freopen("math.in","r",stdin);
	freopen("math.out","w",stdout);
	n=read();
	for(int i=1;i<=n;i++) p[i]=read();
	sort(p+1,p+n+1);
	for(int i=1;i<=n;i++){
		if(f[i-1]<p[i]-1) return write(f[i-1]+1,'\n'),0; 
		f[i]=f[i-1]+p[i];
	}
	return write(f[n]+1,'\n'),0;
}

T2 小L的數列

把乘法化為指數加法,就是挺裸的矩陣快速冪優化了。

注意指數上應模\(998244352\)。由費馬小定理不難得出\(x^{p-1}\equiv x(\mod p)\)

\(code:\)

T2
#include<bits/stdc++.h>
using namespace std;

namespace IO{
	auto read=[]()->int{
		char ch=getchar(); int x=0,f=1;
		while(ch<'0'||ch>'9'){ if(ch=='-') f=-1; ch=getchar(); }
		while(ch>='0'&&ch<='9'){ x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); }
		return x*f;
	};
	auto write=[](int x,int sp)->void{
		char ch[20]; int len=0;
		if(x<0){ x=~x+1; putchar('-'); }
		do{ ch[len++]=(1<<4)+(1<<5)+x%10; x/=10; }while(x);
		for(int i=len-1;~i;--i) putchar(ch[i]); putchar(sp);
	};
	auto ckmax=[](int& x,int y)->void{ x=x<y?y:x; };
	auto ckmin=[](int& x,int y)->void{ x=x<y?x:y; };
} using namespace IO;

const int NN=210,p=998244353,mod=p-1;
int n,k,ans,b[NN],f[NN];
int qpow(int a,int x){
	int res=1;
	for(;x;x>>=1){
		if(x&1) res=1ll*res*a%p;
		a=1ll*a*a%p;
	}
	return res;
}

namespace Matrix{
	struct mat{
		int s[NN][NN];
		mat(){}
		mat(int x){ memset(s,0,sizeof(s)); for(int i=1;i<=k;i++) s[i][i]=x; }
		mat operator*(const mat& a)const{
			mat res=mat(0);
			for(int i=1;i<=k;i++)
				for(int l=1;l<=k;l++)
					for(int j=1;j<=k;j++)
						(res.s[i][j]+=1ll*s[i][l]*a.s[l][j]%mod)%=mod;
			return res;
		}
	}t;
	mat operator^(mat a,int x){
		mat res=mat(1);
		for(;x;x>>=1){
			if(x&1) res=res*a;
			a=a*a;
		}
		return res;
	}
	void prework(){
		for(int i=1;i<k;i++) t.s[i+1][i]=1;
		for(int i=1;i<=k;i++) t.s[i][k]=b[k-i+1];
	}
} using namespace Matrix;

signed main(){
	freopen("seq.in","r",stdin);
	freopen("seq.out","w",stdout);
	n=read(); k=read(); ans=1;
	for(int i=1;i<=k;i++) b[i]=read();
	for(int i=1;i<=k;i++) f[i]=read();
	if(n<=k) return write(f[n],'\n'),0;
	prework(); t=t^(n-k);
	for(int i=1;i<=k;i++)
		ans=1ll*ans*qpow(f[i],t.s[i][k])%p;
	return write(ans,'\n'),0;
}
/*
5 4
1 2 3 4
4 3 2 1

100000 4
1 2 3 4
12 23 34 45

*/

T3 連邊

以黑點為源點做多源最短路。

考慮多加一個白點的貢獻,其實就是白點向前驅連了一條邊。因此做最短路時記錄每個點在最短路中與它連邊最短的前驅,最後累加答案即可。

\(code:\)

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

namespace IO{
	auto read=[]()->int{
		char ch=getchar(); int x=0,f=1;
		while(ch<'0'||ch>'9'){ if(ch=='-') f=-1; ch=getchar(); }
		while(ch>='0'&&ch<='9'){ x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); }
		return x*f;
	};
	auto write=[](int x,int sp)->void{
		char ch[20]; int len=0;
		if(x<0){ x=~x+1; putchar('-'); }
		do{ ch[len++]=(1<<4)+(1<<5)+x%10; x/=10; }while(x);
		for(int i=len-1;~i;--i) putchar(ch[i]); putchar(sp);
	};
	auto ckmax=[](int& x,int y)->void{ x=x<y?y:x; };
	auto ckmin=[](int& x,int y)->void{ x=x<y?x:y; };
} using namespace IO;

const int NN=100010;
int n,m,ans;
bool col[NN];

namespace graph{
	#define x first
	#define y second
	#define mp make_pair
	#define pb push_back
	typedef pair<int,int> PII;
	const int MM=NN<<2;
	priority_queue<PII,vector<PII>,greater<PII>>q;
	int idx,to[MM],nex[MM],w[MM],head[NN],dis[NN],mn[NN];
	bool vis[NN];
	void add(int a,int b,int c){
		to[++idx]=b; nex[idx]=head[a]; head[a]=idx; w[idx]=c;
		to[++idx]=a; nex[idx]=head[b]; head[b]=idx; w[idx]=c;
	}
	void dijstra(){
		memset(dis,0x3f,sizeof(dis));
		memset(mn,0x3f,sizeof(mn));
		for(int i=1;i<=n;i++) if(col[i])
			dis[i]=0, q.push(mp(0,i));
		while(!q.empty()){
			int x=q.top().y,y=q.top().x; q.pop();
			if(vis[x]) continue;
			vis[x]=1;
			for(int i=head[x];i;i=nex[i]){
				int v=to[i];
				if(dis[v]==y+w[i]) ckmin(mn[v],w[i]);
				else if(dis[v]>y+w[i]){
					dis[v]=y+w[i];
					mn[v]=w[i];
					q.push(mp(dis[v],v));
				}
			}
		}
	}
} using namespace graph;

void getans(){
	for(int i=1;i<=n;i++) if(!col[i]){
		if(mn[i]>1e17) puts("impossible"),exit(0);
		ans+=mn[i];
	}
}

signed main(){
	freopen("minimum.in","r",stdin);
	freopen("minimum.out","w",stdout);
	n=read(); m=read();
	for(int i=1;i<=n;i++) col[i]=read();
	for(int a,b,c,i=1;i<=m;i++)
		a=read(),b=read(),c=read(),add(a,b,c);
	dijstra(); getans();
	return write(ans,'\n'),0;
}
/*
5 7
0 1 0 1 0
1 2 2
1 3 1
1 5 17
2 3 1
3 5 18
4 5 3
2 4 5

*/

T4 小L的有向圖

狀壓\(DP\),狀態記已考慮的點集。

每次考慮在列舉到的點集\(S\)的拓撲序後加入一個點\(i\),那麼\(S\)內連向\(i\)的邊可刪可不刪,其它邊必須刪。

找到這個邊數\(x\),將方案數乘上\(2^x\)轉移刷表。

\(code:\)

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

namespace IO{
	auto read=[]()->int{
		char ch=getchar(); int x=0,f=1;
		while(ch<'0'||ch>'9'){ if(ch=='-') f=-1; ch=getchar(); }
		while(ch>='0'&&ch<='9'){ x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); }
		return x*f;
	};
	auto write=[](int x,int sp)->void{
		char ch[20]; int len=0;
		if(x<0){ x=~x+1; putchar('-'); }
		do{ ch[len++]=(1<<4)+(1<<5)+x%10; x/=10; }while(x);
		for(int i=len-1;~i;--i) putchar(ch[i]); putchar(sp);
	};
	auto ckmax=[](int& x,int y)->void{ x=x<y?y:x; };
	auto ckmin=[](int& x,int y)->void{ x=x<y?x:y; };
} using namespace IO;

const int NN=23,MM=470,p=998244353;
int n,m,ans,in[NN],pw[MM],f[1<<22];

signed main(){
	freopen("topology.in","r",stdin);
	freopen("topology.out","w",stdout);
	n=read(); m=read(); pw[0]=f[0]=1;
	for(int a,b,i=1;i<=m;i++)
		a=read(),b=read(),in[b]|=1<<a-1,pw[i]=(pw[i-1]<<1)%p;
	for(int u=0;u<(1<<n);u++)
		for(int i=1;i<=n;i++) if(!(u&(1<<i-1))){
			int tmp=__builtin_popcount(u&in[i]);
			(f[u|(1<<i-1)]+=f[u]*pw[tmp])%=p;
		}
	return write(f[(1<<n)-1],'\n'),0;
}
/*
3 3
1 2
2 3
3 1

*/