1736B - Playing with GCD - CodeForces Solution


math number theory

Please click on ads to support us..

Python Code:

import math
for i in range (int(input())):
    x=int(input())
    a=list(map(int,input().split()))
    b=[0]*(x+1)
    b[0]=a[0]
    b[1]=a[0]
    for i in range(1,x):
        
        b[i+1]=a[i]
        g=math.gcd(b[i],a[i])
        b[i]=b[i]*a[i]//g
        
    check=0
    for i in range(x):
        if math.gcd(b[i],b[i+1])!=a[i]:
            check=1
            break
    if check:print('NO')
    else:print('YES')

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define int  long
#define lll __int128
#define ceil(n, m) (((n) / (m)) + ((n) % (m) ? 1 : 0))
#define endl '\n'
const long long INF = 1ll << 32;
const long double PI = acos(-1);
const int N = 1000001, Mod = 1000000007 , inf = 1e9 , bits = 27;
const int NN = 1e9 , OO = 0x3F3F3F3F;
#define NeedForSpeed ios_base::sync_with_stdio(false) , cin.tie(0),cout.tie(0);
int lcm(int a,int b) {
    int g = __gcd(a, b);
    return (a * b / g);
}
void solve () {
    int n;
    cin >> n;
    vector<int> a(n + 2 , 1), b(n + 2 , 1);
    for (int i = 1;i <= n;i ++){
        cin >> a[i];
    }
    for (int i = 1;i <= n + 1;i++){
            b[i] = lcm (a[i - 1] , a[i]);
    }
    bool ok = true;
    
    for (int i = 1; i <= n; i++){
        if (__gcd(b[i] , b[i + 1]) != a[i])
            ok = false;
    }
    cout << (ok ? "YES" : "NO") << endl;
}
int32_t main() {
    NeedForSpeed
    int test_cases = 1;
    cin >> test_cases;
    while (test_cases--) {
        solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1325D - Ehab the Xorcist
552B - Vanya and Books
1722E - Counting Rectangles
168A - Wizards and Demonstration
168B - Wizards and Minimal Spell
7A - Kalevitch and Chess
912B - New Year's Eve
1537C - Challenging Cliffs
879B - Table Tennis
1674E - Breaking the Wall
1282A - Temporarily unavailable
1366C - Palindromic Paths
336A - Vasily the Bear and Triangle
926A - 2-3-numbers
276D - Little Girl and Maximum XOR
1253C - Sweets Eating
1047A - Little C Loves 3 I
758D - Ability To Convert
733A - Grasshopper And the String
216A - Tiling with Hexagons
1351B - Square
1225A - Forgetting Things
1717A - Madoka and Strange Thoughts
1717B - Madoka and Underground Competitions
61B - Hard Work
959B - Mahmoud and Ehab and the message
802G - Fake News (easy)
1717C - Madoka and Formal Statement
420A - Start Up
1031A - Golden Plate