-
Notifications
You must be signed in to change notification settings - Fork 0
/
div1Dgcd.cpp
219 lines (168 loc) · 4.45 KB
/
div1Dgcd.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <utility>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define l(x) (x << 1) + 1
#define r(x) (x << 1) + 2
#define mid(l, r) ((l + r) >> 1)
#define mp make_pair
#define pb push_back
#define all(a) a.begin(),a.end()
#define pr(n) printf("%d",n)
#define s(n) scanf("%d",&n)
#define debug(x) {cerr <<#x<<" = " <<x<<"\n"; }
#define debug2(x, y) {cerr <<#x<<" = " <<x<<", "<<#y <<" = " <<y ;}
#define ss second
#define ff first
#define m0(x) memset(x,0,sizeof(x))
#define snuke(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
const ll mx_ll = numeric_limits<ll> :: max();
const int mx_int = numeric_limits<int> :: max();
const long double PI = (long double)(3.1415926535897932384626433832795);
inline bool ispow2(int x){return (x!=0 && (x&(x-1))==0);} //0 or 1
int msb(unsigned x){ union { double a; int b[2]; }; a = x; return (b[1] >> 20) - 1023; }
template<class T>
inline void cinarr(T a, int n){ for (int i=0;i<n;++i) cin >> a[i];}
inline int nextint(){ int x; scanf("%d",&x); return x; }
inline ll nextll(){ ll x; scanf("%lld",&x); return x; }
typedef vector<ll> VI;
typedef pair<ll,ll> PII;
// return a % b (positive value)
int mod(int a, int b) {
if( b == 0 )
return a;
return ((a%b)+b)%b;
}
// computes gcd(a,b)
int gcd(int a, int b) {
int tmp;
while(b){a%=b; tmp=a; a=b; b=tmp;}
return a;
}
// computes lcm(a,b)
int lcm(int a, int b) {
return a/gcd(a,b)*b;
}
// returns d = gcd(a,b); finds x,y such that d = ax + by
int extended_euclid(int a,int b,int &x , int &y) {
if (!b) { x=1, y=0; return a; }
ll ret=extended_euclid(b,a%b,x,y), t=x;
x=y, y=t-a/b*x;
return ret;
}
// computes b such that ab = 1 (mod n), returns -1 on failure
int mod_inverse(int a, int n) {
int x, y;
int d = extended_euclid(a, n, x, y);
if (d > 1) return -1;
return mod(x,n);
}
// Chinese remainder theorem (special case): find z such that
// z % x = a, z % y = b. Here, z is unique modulo M = lcm(x,y).
// Return (z,M). On failure, M = -1.
PII chinese_remainder_theorem(int x, int a, int y, int b) {
int s, t;
int d = extended_euclid(x, y, s, t);
cout << "x = " << x << " a = " << a << " y = " << y << " b = " << b << " d = " << d<< " s = " << s << " t = " << t << "\n";
if ( mod(( a-b ) ,d) != 0 )
return make_pair(0, -1);
return make_pair(mod(s*b*x + t*a*y , x*y )/d, x*y/d);
}
// Chinese remainder theorem: find z such that
// z % x[i] = a[i] for all i. Note that the solution is
// unique modulo M = lcm_i (x[i]). Return (z,M). On
// failure, M = -1. Note that we do not require the a[i]'s
// to be relatively prime.
PII chinese_remainder_theorem(const VI &x, const VI &a) {
PII ret = make_pair(a[0], x[0]);
for (int i = 1; i < x.size(); i++) {
ret = chinese_remainder_theorem(ret.second, ret.first, x[i], a[i]);
if (ret.second == -1) break;
}
return ret;
}
// computes x and y such that ax + by = c; on failure, x = y =-1
void linear_diophantine(int a, int b, int c, int &x, int &y) {
int d = gcd(a,b);
if (c%d) {
x = y = -1;
} else {
x = c/d * mod_inverse(a/d, b/d);
y = (c-a*x)/b;
}
}
ll n , m , k;
vector<ll> a;
const ll maxN = 1e12;
ll L = 1 ;
bool solve()
{
L = a[0];
for (int i = 1; i < a.size(); i++)
{
L /= gcd(L,a[i]);
if ( L > n / a[i] )
return false;
L *= a[i];
}
vector<ll> c;
for(int i = 0; i < k; i++)
{
c.pb(mod(-i,a[i]));
}
ll col_num ;
ll row_num = L;
col_num = chinese_remainder_theorem(c,a).ff;
ll l = chinese_remainder_theorem(c,a).ss;
debug(l);
debug(row_num );
debug(col_num);
if ( ( col_num + k ) > m )
return false;
debug(row_num );
debug(col_num);
for(int i = 0; i < k; i++)
{
if( gcd(row_num , col_num + i ) != a[i] )
return false;
}
return true;
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(0);
/*
vector<int> r ;
vector<int> mod;
int n; cin >> n;
for(int i = 0; i < n; i++)
{
ll x , y ; cin >> x >> y;
mod.pb(x);
r.pb(y);
}
cout << chinese_remainder_theorem( r , mod ).ff << "\n";
cout << chinese_remainder_theorem( r , mod ).ss << "\n";
*/
cin >> n >> m >> k;
for(int i = 0 ; i < k ; i++)
{
ll x;
cin >> x;
a.pb(x);
}
if ( solve() )
{
cout << "YES" << "\n"; ;
}
else
{
cout << "NO" << "\n" ;
}
}