wxyww

一个OIER

我们要有最朴素的生活,与最遥远的梦想。即使明日天寒地冻,路远马亡。


cometOJ10C 鱼跃龙门

题目链接

problem

实际上就是对于给定的$n$求一个最小的$x$满足$\frac{x(x+1)}{2}=kn(k\in N^*)$。

solution

对上面的式子稍微变形可得$x(x+1)=2kn$。因为$x$与$(x+1)$互质,所以将$n$质因数分解后,同种质因子肯定都位于$x$或$(x+1)$中。$10^{12}$以内的整数质因数分解后种类不超过$13$种,所以可以暴力枚举每种质因子属于$x$还是$x+1$。

然后分别得到$a$和$b$。下面要使得$bx=ay+1$。扩展欧几里得求解即可。

PS

本题时限$0.5s$,每次询问都$\sqrt{n}$质因数分解是会$TLE$的。所以先预处理质数。然后进行质因数分解。

code

//@Author: wxyww
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<ctime>
#include<cmath>
#include<map>
#include<string>
using namespace std;
typedef long long ll;
const int N = 5000010;
ll read() {
	ll x = 0,f = 1; char c = getchar();
	while(c < '0' || c > '9') {if(c == '-') f = -1;c = getchar();}
	while(c >= '0' && c <= '9') {x = x * 10 + c - '0',c = getchar();}
	return x * f;
}
ll dis[N];
int prmjs,vis[N];
ll n;
ll js,cnt[15];
void fj(ll x) {
	for(int i = 1;dis[i] * dis[i] <= x;++i) {
		if(x % dis[i] == 0) {
			cnt[++js] *= dis[i];
			x /= dis[i];
		}
		while(x % dis[i] == 0) x /= dis[i],cnt[js] *= dis[i];
	}
	if(x != 1) cnt[++js] = x;
}
ll ans;
ll exgcd(ll a,ll b,ll &x,ll &y) {
   if(b == 0) {
      x = 1,y = 0;return a;
  	}
   ll tmp = exgcd(b,a % b,x,y);
   ll t = x;
   x = y; y = t - a / b * y;
   return tmp;
}

int main() {
	int T = read();
	for(int i = 2;i <= 2000000;++i) {
		if(!vis[i]) dis[++prmjs] = i;
		for(int j = 1;j <= prmjs && 1ll * dis[j] * i <= 1000000;++j) {
			vis[dis[j] * i] = 1;
			if(i % dis[j] == 0) break;
		}
	}
	while(T--) {
		n = read() * 2;
		js = 0;
		for(int i = 1;i <= 14;++i) cnt[i] = 1;
		fj(n);
		ans = n * 2;
		ll m = 1 << js;
		for(int i = 0;i < m;++i) {
			ll now = 1;
			for(int j = 0;j < js;++j) if(i >> j & 1) now *= cnt[j + 1];
			ll x,y;
			exgcd(now,n / now,x,y);
			y = y % now;
			if(y >= 0) y -= now;
			ans = min(ans,n / now * -y);
		}
		printf("%lld\n",ans);
	}
	return 0;
}

最近的文章

Noip2018Day1T3 赛道修建

题目链接problem给出一棵有边权的树。一条链的权值定义为该链所经过的边的边权值和。需要选出$m$条链,求$m$条链中权值最小的链的权值最大是多少。solution首先显然二分。然后考虑如何判断二分出来的一个答案$x$是否是可行的。也就是能否选出$m$条链,每条链权值都大于等于$x$。这个其实是贪心。定义直链为从一个某个点的祖先到该点的路径。可以发现每条链要么就是一条直链,要么由两条直链在某个点处合并起来得到。贪心的地方在于,对于每个点肯定都是优先将能合成的直链合成。然后再保证向上传递的...…

贪心,图论继续阅读
更早的文章

AtCoder Beginner Contest 139F Engines

链接problem给出$n$个二元组$(x,y)$。最初位于原点$(0,0)$,每次可以从这$n$个二元组中挑出一个,然后将当前的坐标$(X,Y)$变为$(X+x,Y+y)$,每个二元组只能被选一次。选出一些二元组,使得按照这些二元组移动后与原点的欧几里得距离最大。求这个距离。solution将这$n$个二元组看做$n$个向量。移动方式遵循平行四边形定则。所以两个向量夹角越小,相加形成的和向量模长就越大。所以将这些向量按照极角排序。选择的向量肯定是一个区间。枚举左右端点,求最大值即可。co...…

继续阅读