Skip to content

泉州一中信息学Blog

信息学奥赛技术分享博客

  • 登录
  • 小学oj
  • 中学oj
  • 测试页面1
  • Toggle search form

21 Days Training // Day 12

Posted on 2025年8月6日2025年8月6日 By c2022zyh 21 Days Training // Day 12无评论

T4. leap

Description(Simplified) :
Find the mininum positive interger x that satisfies \frac{x(x+1)}{2} \mid N , i.e. {x(x+1)} \mid 2N .

Wrong idea :

Enumerate the factors of 2N, and find 2 factors p_1, p_2 that satisfies p_2 \equiv \pm 1 \pmod{p_1} .
If, p_2 \equiv 1 \pmod{p_1} , x=p_2-1=kp_1 .
If, p_2 \equiv -1 \pmod{p_1} , x=p_2+1=kp_1 .

For all possiblities, find the mininum one.

Bugs :
This idea did not consider the condition that x=ap_1, x+1=bp_2 .

(Right) idea :
When enumerating all factors, we need to find the mininum positive inteeger a, b that satisfies ap_1+1=bp_2 , i.e. bp_2-ap_1=1 .

For this equation use exgcd to solve it.
After that, calculate x and find the mininum one.
Specially, when p_1=1, current x equals to N-1.

Code :

#include <bits/stdc++.h>
using namespace std;
const long long LIM=1000000;
bitset<LIM+10> isprime, vis;
long long plist[80000], dpf[30];
int pcount=0, dpfcount=0;
set<long long> fac;
map<long long, int> pfac;

void exgcd(long long a, long long b, long long& x, long long& y) {
    if (b == 0) {
        x = 1, y = 0;
        return;
    }
    exgcd(b, a % b, y, x);
    y -= a / b * x;
}

void preprocess() {
    isprime[2]=1, vis[2]=1;
    plist[pcount++]=2;
    for (long long j=4;j<=LIM+10;j+=2) vis[j]=1;
    for (long long i=3;i<=LIM+10;i++) {
        if (vis[i]) continue;
        plist[pcount++]=i;
        isprime[i]=1, vis[i]=1;
        for (long long j=2*i;j<=LIM+10;j+=i) vis[j]=1;
    }
}

void dfs(int L, long long cur) {
    if (L==dpfcount) {
        fac.insert(cur);
        return ;
    }
    long long bk=cur;
    for (int i=0;i<=pfac[dpf[L]];i++) {
        dfs(L+1, bk);
        bk*=dpf[L];
    }
}

void leap() {
    long long N;cin >> N;
    N*=2;
    long long bk=N;
    pfac.clear();
    dpfcount=0;
    for (int i=0;i<pcount;i++) {
        if (!(bk%plist[i])) {
            dpf[dpfcount++]=plist[i];
            pfac[plist[i]]=0;
            while (!(bk%plist[i])) pfac[plist[i]]++, bk/=plist[i];
        }
        if (bk==1) break;
    }
    if (bk!=1) pfac[bk]=1, dpf[dpfcount++]=bk;
    fac.clear();
    dfs(0,1);
    long long ans=LONG_LONG_MAX;
    for (set<long long>::iterator it=fac.begin();it!=fac.end();it++) {
        long long minfac=*it, maxfac;
        if (minfac>sqrt(N)) break;
        maxfac = N/minfac;
        if (minfac==1) {
            ans = min(ans, N-1);
            continue;
        }
        if (__gcd(maxfac, minfac)!=1) continue;
        //minfac*x-maxfac*y=1
        //maxfac*x-minfac*y=1
        long long x1, y1, x2, y2;
        exgcd(minfac, -maxfac, x1, y1);
        exgcd(maxfac, -minfac, x2, y2);
        ans = min(ans, minfac*x1);
        ans = min(ans, maxfac*x2);
    }
    cout << ans << endl;
}

int main() {
    preprocess();
    int C;cin >> C;
    for (int i=0;i<C;i++) leap();
    return 0;
}

Optimize :

  1. When \gcd(p_1, p_2) \ne 1, we can directly skip it. (Line 70).
  2. Preprocess : Calculate all primes between 1 and 1e6. (Line 20~30).
Pages: 1 2 3 4 5 6 7
训练日志

文章导航

Previous Post: 21 Days Training // Day 11
Next Post: GDNOJ – DAY 11

发表回复 取消回复

要发表评论,您必须先登录。

2025年 12月
一 二 三 四 五 六 日
1234567
891011121314
15161718192021
22232425262728
293031  
« 8月    

2024常州 Class Classic OI Problems Contest cqr的长乐集训2023 CZYZ LOC New Game NOI NOIP Password Protected PM_PK Preview Problems Retrospect Selfmade Qusetion STL The end Training Uneasy Problem 蒟蒻 通报

  • 训练日志
  • 链表
  • 入门
  • 模拟
  • dfs序
  • 并查集
  • spfa
  • 最小割
  • 矩阵树定理
  • 仙人掌
  • BSGS
  • 凸包
  • 回文自动机
  • 递推与动归
  • 堆
  • 莫队算法
  • ST表
  • Treap
  • 树套树
  • 可持久化线段树
  • 初赛
  • 搜索
  • 贪心
  • 深度优先搜索
  • 欧拉图
  • dijkstra
  • 费用流
  • 哈夫曼树
  • kruskual
  • 置换
  • 旋转卡壳
  • KMP
  • 区间动归
  • STL
  • 链表
  • 可并堆
  • sply
  • 主席树
  • 可持久化字典树
  • 算法
  • 动态规划
  • 构造
  • 广度优先搜索
  • 最短路
  • floyd
  • 最大流
  • 虚树
  • prim
  • 筛法
  • 半平面交
  • 字典树
  • 背包动归
  • 基础数据结构
  • 分块
  • 线段树
  • 替罪羊树
  • K-DTree
  • 图论
  • 二分法
  • 迭代搜索
  • 拓扑排序
  • 有上下界网络流
  • 生成树
  • 快速幂
  • 后缀数组
  • 树形动归
  • 哈希表
  • 中级数据结构
  • 平衡树
  • 可持久化数据结构
  • 数据结构
  • 三分法
  • 启发式搜索
  • 图的连通
  • 点分治
  • 博弈论
  • AC自动机
  • 状压动归
  • 单调栈
  • 树状数组
  • 高级数据结构
  • OI资料
  • 数学
  • 高精度
  • 差分约束
  • 树上倍增
  • 素数测试
  • 后缀自动机
  • 数位动归
  • 单调队列
  • 新闻
  • 几何
  • 随机化
  • 二分图染色
  • 树链剖分
  • 欧拉函数
  • manacher
  • 斜率优化
  • 离线处理
  • 信息学奥赛学长风采
  • 字符串
  • 二分图匹配
  • prufer编码
  • 卡特兰数
  • 密码学
  • 决策单调
  • 赛后总结
  • 其他
  • 2-SAT
  • 最近公共祖先
  • 矩阵乘法
  • 记忆化搜索
  • 网络流
  • Link cut tree
  • 排列组合
  • 树
  • 高斯消元
  • 乘法逆元
  • 容斥原理
  • 调和级数
  • 概率与期望
  • 模线性方程组
  • 莫比乌斯反演
  • 快速傅里叶变换
  • 扩展欧几里德
  • 最大公约数与最小公倍数

近期文章

  • 中山纪念中学 Day21
  • 中山集训8.15 LAST DAY+集训小结
  • GDNOJ – DAY 18
  • 中山8.14
  • 2025暑假中山集训Day20——8.14

近期评论

归档

  • 2025年8月
  • 2025年7月
  • 2025年2月
  • 2025年1月
  • 2024年11月
  • 2024年10月
  • 2024年9月
  • 2024年8月
  • 2024年7月
  • 2024年3月
  • 2024年2月
  • 2024年1月
  • 2023年12月
  • 2023年11月
  • 2023年10月
  • 2023年9月
  • 2023年8月
  • 2023年7月
  • 2023年3月
  • 2023年2月
  • 2023年1月
  • 2022年12月

Copyright © 2025 泉州一中信息学Blog.

Powered by PressBook WordPress theme