题目链接

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 鱼跃龙门的更多相关文章

  1. Comet OJ - Contest #10 C题 鱼跃龙门

    ###题目链接### 题目大意: 给你一个 x ,让你求出最小的正整数 n 使得 n * (n + 1) / 2  % x == 0 ,即 n * (n + 1)  % 2x == 0 . 分析: 1 ...

  2. Comet OJ - Contest #10 C.鱼跃龙门

    传送门 题意: 求最小的\(x\),满足\(\frac{x(x+1)}{2}\% n=0,n\leq 10^{12}\). 多组数据,\(T\leq 100\). 思路: 直接考虑模运算似乎涉及到二次 ...

  3. Comet OJ - Contest #10 鱼跃龙门 exgcd+推导

    考试的时候推出来了,但是忘了 $exgcd$ 咋求,成功爆蛋~ 这里给出一个求最小正整数解的模板: ll solve(ll A,ll B,ll C) { ll x,y,g,b,ans; gcd = e ...

  4. CometOJ-[Contest #10]鱼跃龙门【exgcd】

    正题 题目链接:https://cometoj.com/problem/1479 题目大意 给出\(n\)求一个最小的\(x(x>0)\)满足 \[\left(\sum_{i=1}^xi\rig ...

  5. 代码规范之争——[个人Week2作业]

    这四个问题均是出自 http://goodmath.scientopia.org/2011/07/14/stuff-everyone-should-do-part-2-coding-standards ...

  6. [Week2 作业] 代码规范之争

    这四个问题均是出自 http://goodmath.scientopia.org/2011/07/14/stuff-everyone-should-do-part-2-coding-standards ...

  7. Kernighan《UNIX 传奇:历史与回忆》杂感

    Brian W. Kernighan 是一个伟大的技术作家,我买了他写的几乎所有书.他近些年的书我买的是 Kindle 电子版,不占地方. 以下是我手上保存的纸版书: Kernighan 的书大多与别 ...

随机推荐

  1. Oracle解析逗号分隔的字符串,或者01110110101此类数据

    -- 1.提取此类数据中的1的索引位置,从1开始    例: 0001100001100 --> 4,5,10,11 create or replace function hazq_instr_ ...

  2. C++标准库之string类型

    stirng类型 简介: C++标准库提供的类型:string 长度可变的字符串 操作简单  仅为包含个人常用函数 头文件 string 类型与其它的标准库类型相同,都需要包含对应的头文件 #incl ...

  3. 卸载/更新HP Client Security Manager失败的解决方案(解决错误1722:软件包存在问题……)

    问题:当卸载较老版本/更新较老版本的HP Client Security Manager时可能会出现下图所示的错误: 解决方案:按Win+R键打开运行窗口,输入regedit回车进入注册表编辑器,依次 ...

  4. 记一次排查jacoco的过程:java.lang.NoSuchMethodException:ApplyOrderdetail.get$jacocoData()

    一.事件: 公司BA今日在st2环境提测试单,添加产品时候一直过不去,找我帮忙看,因为关系比较熟,正好也不是非常忙,我就帮忙定位了下.首先,我在生产环境重现了下,没有问题,在dev环境重现,也没有问题 ...

  5. 配置 Elasticsearch 集群

    Elasticsearch 的安装非常简单,笔者在前文<单机部署 ELK>中已经介绍过了,本文主要介绍集群的配置,并解释常见配置参数的含义. 要配置集群,最简单的情况下,设置下面几个参数就 ...

  6. java之匿名内部类

    Person.java package insof; public class Person extends Object{ String name; static int age; public P ...

  7. Hive 报错 Error while compiling statement: FAILED: ParseException line 1:0 character '' not supported here (state=42000,code=40000)

    Hive报错 Error while compiling statement: FAILED: ParseException line 1:0 character '' not supported h ...

  8. 实现用SQL查询连续发文天数/连续登录天数

    当月最长连续发文天数: //临时:id_time_table: select distinct app_id, from_unixtime(create_date_time, 'yyyy-MM-dd' ...

  9. Android中在fragment中实现点击按钮事件

    在fragment不能直接进行点击事件,需要放到oncreatActivity中 具体方法如下: 需要注意的是import android.support.v4.app.Fragment;导入的这个包 ...

  10. PlayJava Day013

    今日所学: /* 2019.08.19开始学习,此为补档. */ 1.BufferedImage:是Image的一个子类,两者的主要作用就是将一副图片加载到内存中,即图像缓冲区. 对于本地图片: Fi ...