首页 技术 正文
技术 2022年11月20日
0 收藏 612 点赞 4,545 浏览 1394 个字

正题

题目链接:https://cometoj.com/problem/1479


题目大意

给出\(n\)求一个最小的\(x(x>0)\)满足

\[\left(\sum_{i=1}^xi\right)\equiv 0(\mod n)
\]

\(1\leq n\leq 10^{12},1\leq T\leq 100\)


解题思路

转成等比数列求和就是

\[\frac{i(i+1)}{2}\equiv 0(\mod n)\Rightarrow i(i+1)=2kn
\]

从里面获得一下信息,考虑枚举\(2n\)的所有约数\(d\),那么我们有\(xd\times y\frac{2n}{d}=2kn\)。

也就是设\(y\frac{2n}{d}=xd+1\),这个式子我们用\(exgcd\)求出最小解然后所有里面取最小的。

然后是一点优化,首先暴力枚举约数是\(O(\sqrt n)\)的,我们可以质因数分解之后搜索就是\(O(\sigma_0(n))\)的了。

然后因为\(i\)和\((i+1)\)一定互质,所以\(d\)和\(\frac{2n}{d}\)不能有相同的质因子。

这样应该就能过了。


code

#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const ll N=1e6+10;
ll T,n,ans,cnt,tot,pri[N/10],p[30];
bool v[N];
void Prime(){
for(ll i=2;i<N;i++){
if(!v[i])pri[++cnt]=i;
for(ll j=1;j<=cnt&&i*pri[j]<N;j++){
v[i*pri[j]]=1;
if(i%pri[j]==0)break;
}
}
return;
}
ll exgcd(ll a,ll b,ll &x,ll &y){
if(!b){x=1;y=0;return a;}
ll d=exgcd(b,a%b,x,y);
ll z=y;y=x-(a/b)*y;x=z;
return d;
}
void solve(ll x,ll f){
if(x>tot){
if(f==1||f==n)return;
ll a=n/f,b=f,X,Y;
ll d=exgcd(a,b,X,Y);
Y=-Y;
if(X<0){Y+=((-X+b-1)/b)*a;X+=((-X+b-1)/b)*b;}
if(X>0){Y-=(X/b)*a;X-=(X/b)*b;}
if(Y<0){X+=((-Y+a-1)/a)*b;Y+=((-Y+a-1)/a)*a;}
ans=min(ans,min(X*a,Y*b));
return;
}
solve(x+1,f);
solve(x+1,f*p[x]);
return;
}
signed main()
{
Prime();
scanf("%lld",&T);
while(T--){
scanf("%lld",&n);tot=0;
n=n*2;ll x=n;ans=n-1;
for(ll i=1;i<=cnt;i++){
if(x%pri[i]==0){
p[++tot]=1;
while(x%pri[i]==0)
p[tot]*=pri[i],x/=pri[i];
}
}
if(x!=1){p[++tot]=x;}
solve(1,1);
printf("%lld\n",ans);
}
return 0;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,000
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,512
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,358
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,141
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,771
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,849