WA #8 求调
查看原帖
WA #8 求调
1053695
lgx57楼主2025/1/20 20:24
#include<bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define pii pair<int,int>
#define endl '\n'
#define pb push_back
#define ls(p) ((p)<<1)
#define rs(p) ((p)<<1|1)
#define lowbit(x) ((x)&(-(x)))
#define abs(x) ((x)>0?(x):(-(x)))
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))
#define gc getchar
#define pc putchar
#define sz(v) ((int)(v.size()))
using namespace std;
mt19937_64 Rand(chrono::steady_clock::now().time_since_epoch().count());
inline int read(){
	int x=0,f=1;
	char ch=gc();
	while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=gc();}
	while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=gc();}
	return x*f;
}
inline void print(int x){
	if (x<0) pc('-'),x*=-1;
	if (x<10) pc(x+'0');
    else print(x/10),pc(x%10+'0');
}
const int N=1e6+5;
bool a[N];
int sum[N];
int n,x,y;
int cnt=0;
void dfs(int u,int _){
	if (a[u]) return ;
	cnt++;
	a[u]=1;
	if (u+x<=_) dfs(u+x,_);
	if (u-y>=0) dfs(u-y,_);
}
void sol(){
	cin>>n>>x>>y;
	if (n<=2*max(x,y)){
		dfs(0,0);
		int Ans=1;
		for (int i=1;i<=n;i++){
			if (i-x>=0&&a[i-x]) dfs(i,i);
			Ans+=cnt;
		}
		cout<<Ans;
	}
	else{// 这里有问题
		int ans=n+1,m=__gcd(x,y);
		int L=0,R=1e9+5;
		int Ans=0;
		while (L<=R){
			int mid=(L+R)>>1ll;
			if (mid*m-1<=n) Ans=mid,L=mid+1;
			else R=mid-1;
		}
		ans+=Ans*(Ans-1)/2*m;
		int tmp=n-Ans*m+1;
		ans+=tmp*Ans;
		cout<<ans;
	}
}
signed main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int t=1;
	while (t--) sol();
	return 0;
}

input:

42651129 26190 16875

correct answer:

6737492081840
2025/1/20 20:24
加载中...