社区讨论

有谁比我更硬核!

P1473[USACO2.3] 零的数列 Zero Sum参与者 4已保存回复 3

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
3 条
当前快照
1 份
快照标识符
@mik1fpdr
此快照首次捕获于
2025/11/29 16:37
3 个月前
此快照最后确认于
2025/11/29 16:46
3 个月前
查看原帖
CPP
#include <bits/stdc++.h>

using namespace std;
#define rep(i,x,y) for(int i = (x);i <= (y);i++)
#define per(i,x,y) for(int i = (x);i >= (y);i--)
#define ll long long
#define ull unsigned long long
#define db double 
#define PII pair<int,int>
#define inf (1 << 30)
#define lnf (1LL << 60)
#define all(x) x.begin(),x.end()
#define debug puts("?????")
template<class T> void S(T &x){
  x = 0;int f = 1;char ch = getchar();
  while(!(ch >= '0' && ch <= '9')){if(ch == '-') f = -f;ch = getchar();}
  while(ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}
  x *= f;
}
template<class T,class ...A> void S(T &f,A &...a){S(f);S(a...);}
const int N = 9 + 7;
const int P = 998244353;
int T,n;
char opt[N];
int st[N];
int top;
void dfs(int x){
	if(x == n + 1){
		top = 0;
		int ans = 0,num = 0;
		char last_opt = '+';
		for(int i = 2;i <= n;i++){
			num = num * 10 + (i - 1); 
			if(opt[i] == '+' || opt[i] == '-'){
				if(last_opt == '+'){
					st[++top] = num;
				}else{
					st[++top] = -num;
				}
				num = 0;
				last_opt = opt[i];
			}
		}
		num = num * 10 + n;
		if(last_opt == '+')
			st[++top] = num;
		else
			st[++top] = -num;
		int this_is = top;
		while(top){
			ans += st[top--];
		}
		if(ans == 0){
//			for(int i = 1;i <= this_is;i++)
//				printf("%d ",st[i]);
			// printf("\n");
			printf("1");
			for(int i = 2;i <= n;i++){
				printf("%c%lld",opt[i],i); 
			}
			printf("\n");	
		}
		return;
	}
	opt[x] = ' ';
	dfs(x+1);
	opt[x] = '+';
	dfs(x+1);
	opt[x] = '-';
	dfs(x+1);
}
void solve(){
	scanf("%lld",&n);
	dfs(2);
	return;
}
signed main(){
  // S(T);
  T = 1;
  while(T--){
    solve();
  }
  return 0;
}

回复

3 条回复,欢迎继续交流。

正在加载回复...