1459C. Row GCD(经典数论)

GCD更相减损术算法解析
本文介绍了一个利用更相减损术求解多个数的最大公约数(GCD)的问题。通过分析给出的两个整数序列,文章提供了一种高效算法来计算每个序列元素加固定数后的最大公约数。

You are given two positive integer sequences a1,…,an and b1,…,bm. For each j=1,…,m find the greatest common divisor of a1+bj,…,an+bj.

Input
The first line contains two integers n and m (1≤n,m≤2⋅105).

The second line contains n integers a1,…,an (1≤ai≤1018).

The third line contains m integers b1,…,bm (1≤bj≤1018).

Output
Print m integers. The j-th of them should be equal to GCD(a1+bj,…,an+bj).

Example

input

4 4
1 25 121 169
1 2 7 23

output

2 3 8 24

题意:题目中有m次询问,每次要求出n个数加上一个数之后,这n个数的gcd。

分析:此题用到了gcd的更相减损术gcd(a,b)=gcd(a,b−a);gcd(a,b,c) = gcd(a,b-a,c-b)那么推广到这个数组,gcd(a1+bj,…,an+bj)=gcd(a1+bj,a2-a1,a3-a2,…,an-a(n-1))。所以我们只需先求出g=gcd(a2-a1,a3-a2,…)。而最终答案就是gcd(g,a1+bj);

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;
long long int a[200005],b[200005];
long long int c[200005];
int main()
{
    int n,m;
    while(~scanf("%d %d",&n,&m)){
        for(int i=0;i<n;i++){
            scanf("%lld",&a[i]);
            if(i==0)continue;
            c[i]=a[i]-a[i-1];
        }
        long long int ans=c[1];
        for(int i=1;i<n;i++)ans=__gcd(ans,c[i]);
        for(int j=0;j<m;j++){
            scanf("%lld",&b[j]);
            cout<<abs(__gcd(ans,b[j]+a[0]))<<" ";
        }
        cout<<endl;
    }
    return 0;
}

相关题目
也是与gcd更相减损有关的题

ll gcd2(ll a, ll b) { if (a < 0) a = -a; if (b < 0) b = -b; while (b != 0) { ll temp = b; b = a % b; a = temp; } rt a; } //计算三元最大公因数(gcd2) ll gcd3(ll a, ll b, ll c) { rt gcd2(gcd2(a, b), c); } //计算n元最大公因数(数组[1,n);嵌gcd2) ll gcdn(ll arr[], in n) { if (n == 0) rt 0; ll result = arr[0]; for (in i = 1; i < n; ++i) { result = gcd2(result, arr[i]); if (result == 1) br; } rt result; } //计算二元最小公倍数(gcd2) ll lcm2(ll a, ll b) { if (a == 0 || b == 0) rt 0; ll ga = a < 0 ? -a : a; ll gb = b < 0 ? -b : b; ll g = gcd2(ga, gb); rt ga / g * gb; } //计算三元最小公倍数(嵌lcm2) ll lcm3(ll a, ll b, ll c) { rt lcm2(lcm2(a, b), c); } //计算n元最小公倍数(数组[1,n);嵌lcm2) ll lcmn(ll arr[], in n) { if (n == 0) rt 1; ll result = arr[0]; for (in i = 1; i < n; ++i) { result = lcm2(result, arr[i]); if (result == 0) br; } rt result; } //计算n元最小公倍数(数组[1,n);嵌lcm2;取模) ll lcmn_mod(ll arr[], in n) { if (n == 0) rt 1; ll result = arr[0]; for (in i = 1; i < n; ++i) { result = lcm2(result, arr[i]); if (result == 0) br; result %= MOD; } rt result; }in matrix_rank(in A[][MAX_LEN], in m, in n) { in rank = 0; for (in col = 0; col < n && rank < m; col++) { in pivot_row = -1; for (in row = rank; row < m; row++) { if (A[row][col] != 0) { pivot_row = row; break; } } if (pivot_row == -1) continue; if (pivot_row != rank) { for (in j = 0; j < n; j++) { in temp = A[rank][j]; A[pivot_row][j] = A[rank][j]; A[rank][j] = temp; } } for (in row = rank + 1; row < m; row++) { if (A[row][col] == 0) continue; in a = A[rank][col]; in b = A[row][col]; in l = lcm(a, b); if (l == 0) continue; in mult1 = l / a; in mult2 = l / b; for (in j = col; j < n; j++) { A[row][j] = mult2 * A[row][j] - mult1 * A[rank][j]; } } rank++; } rt rank; }
最新发布
12-01
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

a碟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值