brute force dsu meet-in-the-middle number theory *1300

Please click on ads to support us..

Python Code:

from math import gcd
n,m=map(int,input().split())
gcdd=gcd(n,m)
l=list(map(int,input().split()));r=[0]*gcdd
for i in l[1:]:
 r[i%gcdd]=1
l1=list(map(int,input().split()))
for j in l1[1:]:
 r[j%gcdd]=1
for i in r:
  if i==0:exit(print("No"))
print("Yes")

C++ Code:

// radhe Krishna // 
     
#include<bits/stdc++.h>
using namespace std;

#define nl "\n"
#define ll long long
#define ff first
#define ss second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define al(x) x.begin(), x.end()
#define Test int t;cin>>t;while(t--)
#define rep(i,n) for(ll i=0;i<n;i++)
#define mpp map<ll,ll>
#define vi(type) vector<type>
#define vv  vector<vector<ll>>
#define vp vector<pair<ll,ll>>
#define uq(a) (a).erase(unique((a).begin(),(a).end()),(a).end())

ll dx[4]={-1,0,1,0};
ll dy[4]={0,1,0,-1};
  
void fast()
{
   ios_base::sync_with_stdio(0);
   cin.tie(0);
   cout.tie(0);
} 

void solve(){
  ll n,m; cin>>n>>m;
  vi(ll) a(n),b(m);
  mpp mpa,mpb;
  ll by; cin>>by;
  rep(i,by) {
    cin>>a[i];
    mpa[a[i]]=1;
  }
  ll gy; cin>>gy;
  rep(i,gy) {
    cin>>b[i];
    mpb[b[i]]=1;
  }

  for(ll i=0;i<10000;i++){
    if(mpa[i%n] || mpb[i%m]){
      mpa[i%n]=1;
      mpb[i%m]=1;
    }
  }

  ll fl1=0,fl2=0;
  rep(i,n){
    if(mpa[i]) fl1++;
  }
  rep(i,m){
    if(mpb[i]) fl2++;
  }
  // cout<<fl1<<" "<<fl2;
  if(fl1==n && fl2==m) cout<<"YES\n";
  else cout<<"NO\n";
}

int main()
{
    fast();  
    solve();
    // Test
    // {    
    //     solve();
    // }
    return 0;
}


Comments

Submit
0 Comments
More Questions

406. Queue Reconstruction by Height
380. Insert Delete GetRandom O(1)
332. Reconstruct Itinerary
368. Largest Divisible Subset
377. Combination Sum IV
322. Coin Change
307. Range Sum Query - Mutable
287. Find the Duplicate Number
279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II
238. Product of Array Except Self
229. Majority Element II
222. Count Complete Tree Nodes
215. Kth Largest Element in an Array
198. House Robber
153. Find Minimum in Rotated Sorted Array
150. Evaluate Reverse Polish Notation
144. Binary Tree Preorder Traversal
137. Single Number II
130. Surrounded Regions
129. Sum Root to Leaf Numbers
120. Triangle
102. Binary Tree Level Order Traversal
96. Unique Binary Search Trees
75. Sort Colors
74. Search a 2D Matrix
71. Simplify Path