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;
}
CometOJ10C 鱼跃龙门的更多相关文章
- Comet OJ - Contest #10 C题 鱼跃龙门
###题目链接### 题目大意: 给你一个 x ,让你求出最小的正整数 n 使得 n * (n + 1) / 2 % x == 0 ,即 n * (n + 1) % 2x == 0 . 分析: 1 ...
- Comet OJ - Contest #10 C.鱼跃龙门
传送门 题意: 求最小的\(x\),满足\(\frac{x(x+1)}{2}\% n=0,n\leq 10^{12}\). 多组数据,\(T\leq 100\). 思路: 直接考虑模运算似乎涉及到二次 ...
- Comet OJ - Contest #10 鱼跃龙门 exgcd+推导
考试的时候推出来了,但是忘了 $exgcd$ 咋求,成功爆蛋~ 这里给出一个求最小正整数解的模板: ll solve(ll A,ll B,ll C) { ll x,y,g,b,ans; gcd = e ...
- CometOJ-[Contest #10]鱼跃龙门【exgcd】
正题 题目链接:https://cometoj.com/problem/1479 题目大意 给出\(n\)求一个最小的\(x(x>0)\)满足 \[\left(\sum_{i=1}^xi\rig ...
- 代码规范之争——[个人Week2作业]
这四个问题均是出自 http://goodmath.scientopia.org/2011/07/14/stuff-everyone-should-do-part-2-coding-standards ...
- [Week2 作业] 代码规范之争
这四个问题均是出自 http://goodmath.scientopia.org/2011/07/14/stuff-everyone-should-do-part-2-coding-standards ...
- Kernighan《UNIX 传奇:历史与回忆》杂感
Brian W. Kernighan 是一个伟大的技术作家,我买了他写的几乎所有书.他近些年的书我买的是 Kindle 电子版,不占地方. 以下是我手上保存的纸版书: Kernighan 的书大多与别 ...
随机推荐
- JVM运行时数据区-详细结构图
- 记一次在node.js中使用crypto的createCipheriv方法进行加密时所遇到的坑
Node.js的crypto模块提供了一组包括对OpenSSL的哈希.HMAC.加密.解密.签名,以及验证等一整套功能的封装.具体的使用方法可以参考这篇文章中的描述:node.js_crypto模块. ...
- python创建多维字典方法
python不直接创建多维字典,需要逐层判断不存在创建,存在追加: 例如: 不能直接 dictName['key1']['key2']['key3']['key4']['key5'] = ['123' ...
- django admin显示多对多字段ManyToManyField
参考文档https://jingyan.baidu.com/article/4e5b3e190f55c591901e24b3.html admin.py from .models import *cl ...
- crontab -e 报错(E518: Unknown option: foldenable)
crontab 默认编辑器为vi,不支持foldenable #crontab -e Error detected while processing /root/.vimrc: line : E518 ...
- 09-Node.js学习笔记-异步编程
同步API,异步API 同步API:只有当前API执行完成后,才能继续执行下一个API console.log('before'); console.log('after'); 异步API:当前API ...
- vue 父子父组件通过props传父页面请求后的数据
父子父组件通过props传父页面请求后的数据,则在父页面的子组件上加上判断数据是否存在即可,如下 <gl-line-bar v-if="oneWeekBetEcharts" ...
- 【linux命令】chgrp改变文件或目录的属组
在lunix系统里,文件或目录的权限的掌控以拥有者及所诉群组来管理.可以使用chgrp指令取变更文件与目录所属群组,这种方式采用群组名称或群组识别码都可以.Chgrp命令就是change group的 ...
- kubernetes部署高可用redis
本文redis通过helm搭建,提供redis高可用完整的编排,关于Helm的搭建和使用请查看文章<helm的搭建及使用>,其中前一章介绍了Helm搭建,并提供了Helm搭建Harbor的 ...
- js 导航栏多项点击显示下拉菜单代码
<!DOCTYPE html> <html> <head> <title>Dropdown</title> <!--<link ...