首页 > 其他分享 >Educational Codeforces Round 139 D. Lucky Chains

Educational Codeforces Round 139 D. Lucky Chains

时间:2022-12-22 21:12:38浏览次数:64  
标签:Educational gcd int 样例 Lucky Codeforces include cout

Lucky Chains

题面翻译

给定两个数·a, b,(a, b给到了1e7)执行如下语句:

while(gcd(a, b) == 1) a++, b++, cnt++ ;
求出cnt的值。

样例 #1

样例输入 #1

4
5 15
13 37
8 9
10009 20000

样例输出 #1

0
1
-1
79

分析:

gcd的性质:gcd(a, b) == gcd(a, b - a)
因此gcd(a + k, b + k) == gcd(a + k, b - a)至此a - b为定值,
因此我们只需要判断出对于每一个b - a的质因子p,(a + k) % p == 0时k的最小值,
也就是求p - a % p的最小值

  • 这里用到了分解质因子,首先线性筛一遍1e7内的质数,有3200 * 3200 > 1e7

线性筛+分解质因子O(500n)
参考解析
参考证明

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define mst(x, y) memset(x, y, sizeof x)
#define endl '\n'
#define FAST ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
const int N = 100010, INF = 0x3f3f3f3f3f3f3f3f, MOD = 1e9 + 7;
int T;
int a, b;
int primes[N], cnt;
bool st[N];
void init()
{
    for (int i = 2; i <= 3200; i++)
    {
        if (!st[i])
            primes[cnt++] = i;
        for (int j = 0; primes[j] * i < N; j++)
        {
            st[primes[j] * i] = true;
            if (i % primes[j] == 0)
                break;
        }
    }
}
void solve()
{
    cin >> a >> b;
    if (a > b)
        swap(a, b);
    if (b - a == 1)
    {
        cout << "-1" << endl;
        return;
    }
    else if (__gcd(a, b) != 1)
    {
        cout << "0" << endl;
        return;
    }
    else
    {
        int t = b - a;
        int res = INF, curr = 0;
        while (primes[curr] <= sqrt(t))
        {
            if (t % primes[curr])
            {
                curr++;
                continue;
            }
            while (t % primes[curr] == 0)  // 分解质因子
                t /= primes[curr];
            res = min(res, primes[curr] - a % primes[curr]);
            curr++;
        }
        if (t > 1)
            res = min(res, t - a % t);
        cout << res << endl;
    }
}
signed main()
{
    FAST;
    init();
    cin >> T;
    while (T--)
        solve();
    return 0;
}

标签:Educational,gcd,int,样例,Lucky,Codeforces,include,cout
From: https://www.cnblogs.com/Aidan347/p/16999573.html

相关文章

  • Codeforces Round #652 (Div. 2) C-E
    RationalLee样例#1样例输入#13421137171362101010101111334410000000001000000000100000000010000000001111样例输出#1484280000......
  • Codeforces Round #785 (Div. 2) A-C
    ProblemA题意问给一个长度为2的小写字符串,字符串从ab开始,然后第一个位置和第二个位置上的字符不能相等,问按照这个方式排序,给出的字符串是第几个然后这道题首先分情况讨......
  • Codeforces Round #840 (Div. 2)
    A题意:给定n个整数,可以交换任意两个数二进制上的某一位。求任意操作次数后数组中最大值与最小值的最大差。核心思路:这个思路还是很显然的大胆的猜结论,贪心的考虑每一个......
  • [Codeforces Round #836 (Div. 2)C
    C这一次呢,题目要求让a[1]=x,a[n]=1,我们需要找到一个最小的序列使得每一个a[i]都可以被它的下标整除,没找到就是输出-1我第一次是想着先让a[1]=x,a[n]=1,然后在中间一个一......
  • Codeforces Global Round 20--F1
    F1.ArrayShuffling结论交换的次数=点数-循环圈的数量所以只需要构造尽量多的循环圈,圈上的各个元素都是不相同的就可以了。代码#include<bits/stdc++.h>usingname......
  • Codeforces Round #840 (Div. 2) and Enigma 2022 - Cybros LNMIIT(持续更新)
    Preface有史以来打的最爆炸的一场,只写了AB一个原因是最近由于得了新冠导致疏于锻炼,脑子和手都有点不在状态另一个原因就是没去想C去开一眼感觉很naive的D(事实确实很naiv......
  • Codeforces Round #840 (Div. 2) C
    这一道题也是我没想到的题目大意是这样的:可以选择i,j(i<j),我们可以把i到j包括i,j的元素换成abs(a[i],a[j]),并且我们需要的答案就是经过一系列这样的操作,使得这个数组的......
  • Codeforces 1763 F Edge Queries 题解
    题目链接先观察满足题目中给出的限制的图有什么特点。先看\(C_u\),它指的是所有与\(u\)在同一个简单环内的节点。发现一个点v在\(C_u\)中,当且仅当\(u,v\)点双连通。关于点......
  • Codeforces Round #835 (Div. 4) ABCDEF(二分)
    https://codeforces.com/contest/1760【赛时A-E代码】A.MediumNumber题目大意:三个数字,求第二大的数字。input952614342021123111912108206......
  • Codeforces 1774
    A-AddPlusMinusSign简单题,直接\(\tt-\)和\(\tt+\)交替就好了。缺省源没有。intsolve(){ intn=getInt(); strings; cin>>s; boolb=(s[0]==1......