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 的书大多与别 ...
随机推荐
- C++调用bat并实现传值
1.设置环境变量,这一类为路径 C++ void bat(const string& sDirC, const string& sDirD) { char Ddir[256]; Ddi ...
- 关于for循环中使用setTimeout的四种解决方案
我们先来简单了解一下setTimeout延时器的运行机制.setTimeout会先将回调函数放到等待队列中,等待区域内其他主程序执行完毕后,按时间顺序先进先出执行回调函数.本质上是作用域的问题. 因此 ...
- 探索Dynamics 365 用户能够登录使用的最小权限需求
我是微软Dynamics 365 & Power Platform方面的工程师罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面 ...
- 一些webpack常见编译报错的解决方案
重新安装依赖可以解决80%的webpack编译报错问题. rm -rf node_modules rm package-lock.json npm cache clear --force npm in ...
- webpack css模块化和ant-design按需加载冲突
其实具体出现了什么问题,我也记得不清楚了,今天突然回想起来必须记录一下,一个思想就是用exclude将node_module目录下的文件排除,网上有很多相关例子,但不知是不是因为时间久远,都不生效,无 ...
- IDEA新建Spring配置文件的方法
IDEA创建Spring Config 选择项目文件右键 输入文件名称即可 applicationContext.xml
- 【计算机网络】ISO/OSI 网络体系结构
ISO/OSI 网络体系结构 计算机网络是相当复杂的系统,相互通信的两个计算机系统必须高度协调才能正常工作.为了设计这样复杂的计算机网络,人们提出了将网络分层的方法.分层可将庞大而复杂的问题转换为若干 ...
- 文件迁移到FileTable中
看此文档前,先参考一下文档 https://blog.csdn.net/downmoon/article/details/24374609 环境:阿里云ECS SQL Server 2017 + De ...
- sass参考手册
http://sass.bootcss.com/docs/sass-reference/
- Objective-C中 #include 和 #import 的区别
由于 Objective-C 兼容 C 语言,所以导入文件时, 可以使用 #include,也可以使用 #import (Objective-C 新增的) 如: #include <stdio. ...