跳到正文

算法·理论

CSP-J/S2025第一轮代码

Steady_Steve约 6 分钟
原文

这是 2025年9月25日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2025年9月24日
收录
2025年12月12日
更新
2026年11月15日
篇幅
约 6 分钟

版本

  1. v6当前
  2. v5
  3. v4
  4. v3
更早的 2 个版本
  1. v2
  2. v1

CSP-J

阅读程序第一题:

C++
#include<algorithm>
#include<cstdio>
#include<cstring>
inline int gcd(int a,int b) {
	if(b==0)
		return a;
	return gcd(b,a%b);
}
int main() {
	int n;
	scanf("%d",&n);
	int ans=0;
	for(int i=1; i<=n; ++i) {
		for(int j=i+1; j<=n; j++) {
			for(int k=j+1; k<=n; k++) {
				if(gcd(i,j)==1&&gcd(j,k)==1&&gcd(i,k)==1) {
					++ans;
				}
			}
		}
	}
	printf("%d\n",ans);
	return 0;
}

CSP-S

阅读程序第一题:

C++
#include<algorithm>
#include<cstdio>
#include<cstring>
bool flag[27];
int n;
int p[27];
int ans=0;
void dfs(int k){
	if(k==n+1){
		++ans;
		return;
	}
	for(int i=1;i<=n;++i){
		if(flag[i]) continue;
		if(k>1&&i==p[k-1]+1) continue;
		p[k]=i;
		flag[i]=true;
		dfs(k+1);
		flag[i]=false;
	}
	return;
}
int main()
{
	scanf("%d",&n);
	dfs(1);
	printf("%d\n",ans);
	return 0;
} 

阅读程序第二题:

C++
#include<algorithm>
#include<cstdio>
#include<cstring>
int cnt_broken=0;
int cnt_check=0;
int n,k;
inline bool check(int h){
	printf("now check:%d\n",h);
	++cnt_check;
	if(cnt_broken==2){
		printf("You have no egg!\n");
		return false;
	}
	if(h>=k){
		++cnt_broken;
		return true;
	} else{
		return false;
	}
} 
inline bool assert_ans(int h){
	if(h==k){
		printf("You are Right using %d checks\n",cnt_check);
		return true;
	}else{
		printf("Wrong answer!\n");
		return false;
	}
}
inline void guess1(int n){
	for(int i=1;i<=n;i++){
		if(check(i)){
			assert_ans(i);
			return;
		}
	}
}
inline void guess2(int n){
	int w=0;
	for(w=1;w*(w+1)/2<n;++w)
		;
	for(int ti=w,nh=w; ;--ti,nh+=ti,nh=std::min(nh,n)){
		if(check(nh)){
			for(int j=nh-ti+1;j<nh;++j){
				if(check(j)){
					assert_ans(j);
					return;
				}
			}
			assert_ans(nh);
			return;
		}
	}
}
int main(){
	scanf("%d%d",&n,&k);
	int t;
	scanf("%d",&t);
	if(t==1){
		guess1(n);
	}else{
		guess2(n);
	}
	return 0;
}

完善程序第一题:

C++
#include<algorithm>
#include<iostream>
#include<queue>
#include<vector>
using namespace std;

const long long INF=1e18;

struct Edge{
	int to;
	int weight;
}; 

struct State{
	long long dist;
	int u;
	int used_freebie;
	bool operator>(const State &other) const{
		return dist>other.dist;
	}
};

int main(){
	int n,m,s,t;
	cin>>n>>m>>s>>t;
	
	vector<vector<Edge>> adj(n+1);
	for(int i=0;i<m;++i){
		int u,v,w;
		cin>>u>>v>>w;
		adj[u].push_back({v,w});
		adj[v].push_back({u,w});
	} 
	
	vector<vector<long long>> d(n+1,vector<long long>(2,INF));
	priority_queue<State,vector<State>,greater<State>> pq;
	
	d[s][0]=0;
	pq.push({0,s,0});
	
	while(!pq.empty()){
		State current=pq.top();pq.pop();
		long long dist=current.dist;
		int u=current.u;
		int used=current.used_freebie;
		
		if(dist>d[u][used]){
			continue;
		}
		
		for(const auto &edge : adj[u]){
			int v=edge.to;
			int w=edge.weight;
			
			if(d[u][used]+w<d[v][used]){
				d[v][used]=d[u][used]+w;
				pq.push({d[v][used],v,used});
			}
			
			if(used==0){
				if(d[u][0]<d[v][1]){
					d[v][1]=d[u][0];
					pq.push({d[v][1],v,1});
				}
			}
		}
		
	}
	
	cout<<min(d[t][0],d[t][1]);
	return 0;
}