CodeForces - 1459C Row GCD (思维+数学)

简介: 笔记

Row GCD


题意

对于两个正整数序列 a 1 、 a 2 、 a 3 、 … a n和 b 1 、 b 2 、 b 3 … b m

对于每个 j = 1 、 2 、 … j找到 a 1 + b j 、 a 2 + b j + a 3 + b j 、 … a n + b j

的最大公约数



思路

9.png


代码

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define mod 1000000007
using namespace std;
typedef long long LL;
typedef pair<int, int>PII;
inline LL gcd(LL a, LL b) { return b ? gcd(b, a % b) : a; }
const int N = 200010;
int n, m;
LL a[N], b[N];
void solve() {
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) scanf("%lld", &a[i]);
  for (int i = 1; i <= m; ++i) scanf("%lld", &b[i]);
  LL res = 0;
  for (int i = 2; i <= n; ++i) {
    res = gcd(res, a[i] - a[i - 1]);
  }
  for (int j = 1; j <= m; ++j) {
    cout << abs(gcd(res, a[1] + b[j])) << " ";
  }
  cout << endl;
}
int main() {
  //int t; cin >> t;
  //while (t--)
    solve();
  return 0;
}


目录
相关文章
|
8月前
codeforces319——B. Psychos in a Line(思维+单调栈)
codeforces319——B. Psychos in a Line(思维+单调栈)
112 0
codeforces319——B. Psychos in a Line(思维+单调栈)
|
人工智能
每天两道 CodeForces 构造/思维题 (day5)
每天两道 CodeForces 构造/思维题 (day5)
每天两道 CodeForces 构造/思维题 (day5)
CodeForces - 1312D Count the Arrays(思维+组合数学)
CodeForces - 1312D Count the Arrays(思维+组合数学)
137 0
codeforces1013——D. Chemical table(思维+转化+并查集)
codeforces1013——D. Chemical table(思维+转化+并查集)
96 0
|
Java Shell
Codeforces Round #746 (Div. 2) D - Hemose in ICPC ?(交互 二分 欧拉序)
Codeforces Round #746 (Div. 2) D - Hemose in ICPC ?(交互 二分 欧拉序)
164 0
|
机器学习/深度学习 Java
Codeforces Round #748 (Div. 3) D2. Half of Same(数学 枚举 思维)
Codeforces Round #748 (Div. 3) D2. Half of Same(数学 枚举 思维)
112 0
|
机器学习/深度学习
HDU2376——Average distance(思维+树形DP)
HDU2376——Average distance(思维+树形DP)
100 0