QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#676701 | #5465. Maximum GCD | shohan2032 | WA | 9ms | 4128kb | C++23 | 8.0kb | 2024-10-25 23:17:10 | 2024-10-25 23:17:10 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
//Debug Function-->works for vector,map,set(dbg(stl_name)),,arrar(dbg(array_name,range))
template < typename F, typename S >ostream& operator << ( ostream& os, const pair< F, S > & p ) {return os << "(" << p.first << ", " << p.second << ")";}
template < typename T >ostream &operator << ( ostream & os, const vector< T > &v ) {os << "{";for(auto it = v.begin(); it != v.end(); ++it) {if( it != v.begin() ) os << ", ";os << *it;}return os << "}";}
template < typename T >ostream &operator << ( ostream & os, const set< T > &v ) {os << "[";for(auto it = v.begin(); it != v.end(); ++it) {if( it != v.begin() ) os << ", ";os << *it;}return os << "]";}
template < typename T >ostream &operator << ( ostream & os, const multiset< T > &v ) {os << "[";for(auto it = v.begin(); it != v.end(); ++it) {if( it != v.begin() ) os << ", ";os << *it;}return os << "]";}
template < typename F, typename S >ostream &operator << ( ostream & os, const map< F, S > &v ) {os << "[";for(auto it = v.begin(); it != v.end(); ++it) {if( it != v.begin() ) os << ", ";os << it -> first << " = " << it -> second ;}return os << "]";}
#define dbg(args...) do {cerr << #args << " : "; arif(args); } while(0)
clock_t tStart = clock();
#define timeStamp dbg("Execution Time: ", (double)(clock() - tStart)/CLOCKS_PER_SEC)
void arif () {cerr << endl;}
template <typename T>void arif( T a[], int n ) {for(int i = 0; i < n; ++i) cerr << a[i] << ' ';cerr << endl;}
template <typename T, typename ... hello>void arif( T arg, const hello &... rest) {cerr << arg << ' ';arif(rest...);}
#define db long long
#define ll long long
#define int ll
#define vi vector<int>
#define vs vector<string>
#define vpi vector<pair<int, int>>
#define vpd vector<pair<double, double>>
#define vps vector<pair<string, string>>
#define mp(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define pp(x) pop_back(x)
#define fr(cont) for (auto &i : (cont))
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.rbegin(), cont.rend()
#define mo(a, b) (((a) % (b)) + (b)) % (b)
#define endl '\n'
// constants...
const double pi = acos(-1);
const ll mod = 1000000007;//998244353
const int MXS = 2e5 + 5;
const ll MXI = 1e9 + 5;
const ll MXL = 1e18 + 5;
const ll INF = 1e9 + 5;
const ll INFLL = 1e18 + 5;
const ll eps = 1e-9;
// functions...
ll gcd(ll a, ll b){while (b){a %= b;swap(a, b);}return a;}
ll lcm(ll a, ll b) { return (a / gcd(a, b) * b); }
ll ncr(ll a, ll b){ll x = max(a - b, b), ans = 1;for (ll K = a, L = 1; K >= x + 1; K--, L++){ans = ans * K;ans /= L;}return ans;}
ll egcd(ll a, ll b, ll &x, ll &y){if (a == 0){x = 0;y = 1;return b;}ll x1, y1;ll d = egcd(b % a, a, x1, y1);x = y1 - (b / a) * x1;y = x1;return d;}
// ll fact[2000005];
// ll ncr_mod(ll n,ll r) {return (((fact[n]*inverse_mod(fact[r]))%mod)*inverse_mod(fact[n-r]))%mod;}
db pytha(db x1, db x2, db y1, db y2) { return sqrt((pow((x2 - x1), 2)) + (pow((y2 - y1), 2))); } /// for two ending points pythagoras law
double make_radian(double x) { return (x * pi) / 180; }
double make_degree(double x) { return (x * 180) / pi; }
ll binmul(ll a, ll b, ll p){ll res = 0ll;while (b){if (b & 1)res = (res + a) % p, b--;else a = (a + a) % p, b /= 2;}return res;} //(a*b)%p
ll binpow(ll a, ll b, ll p){ll res = 1ll;while (b){if (b & 1)res = binmul(res, a, p), b--;else a = binmul(a, a, p), b /= 2;}return res;} //(a^b)%p
ll inverse(ll a, ll p) { return binpow(a, p - 2, p); } //(a^-1)%p == (a^p-2)%p
string decToBinary(int n){string s;int i = 0;while (n > 0){s = to_string(n % 2) + s;n = n / 2;i++;}return s;}
ll binaryToDecimal(string n){string num = n;ll dec_value = 0;int base = 1;int len = num.length();for (int i = len - 1; i >= 0; i--){if (num[i] == '1')dec_value += base;base = base * 2;}return dec_value;}
ll factorial(int n){long long fact = 1; int md = 998244353;for (int i = 1; i <= n; i++){fact = (fact * i) % md;}return fact;}
void substring(string s){for (int i = 0; i <= s.size(); i++){for (int j = 0; j <= s.size() -i; j++){int l = j + i - 1;for (int k = i; k <= l; k++)cout << s[k];cout << endl;}}}
void permutation(vector<ll>v){sort(all(v));cout << "All possible permutations with the elements:\n";do{for(int i = 0;i < v.size();i++)cout << v[i] << " ";cout << endl;}while (next_permutation(all(v)));}//total permutation will be n!
bool powerOftwo(ll x){if(x == 0)return false;else return !(x&(x-1));}
ll powss(ll a, ll p){if(p==0) return 1;if(p==1) return a;else if(p&1) {return a*powss(a*a,p/2);}else {return powss(a*a,p/2);}}
string big_add(string a,string b){string ans;if(a.size() > b.size()){while(b.size() != a.size())b = '0'+b;}else{while(a.size() != b.size())a = '0'+a;}int carry = 0;for(int i = a.size()-1;i >= 0;i--){int x = a[i]-'0';int y = b[i]-'0';int sum = x+y+carry;if(sum >= 10){carry = 1;sum %= 10;}else carry = 0;ans += sum+'0';}if(carry)ans += '1';reverse(all(ans));return ans;}
string big_sub(string a,string b){string ans;if(a.size() > b.size()){while(b.size() != a.size())b = '0'+b;}else{while(a.size() != b.size())a = '0'+a;}int carry = 0;for(int i = a.size()-1;i >= 0;i--){int x = a[i]-'0';int y = b[i]-'0';if(carry)y++;int f = 0;if(x < y){x += 10;f = 1;}int sub = x-y;//x theke y biyog(a string theke b string biyog)
ans += sub+'0';if(f)carry = 1;else carry = 0;}while(ans.back() == '0')ans.pop_back();reverse(all(ans));return ans;}
int charToint(char ch){return ch-'0';}
char intTochar(int n){return 48+n;/*return '0'+n;*/}
//x theke d distance poriman clockwise agabe,n hocche total cell
int c_w(int x,int d,int n){x += d;x %= n;if(!x)x = n;return x;}
//x theke d distance poriman counter-clockwise pichabe,n hocche total cell
int cc_w(int x,int d,int n){x = (((x-d)%n)+n)%n;if(!x)x = n;return x;}
bool cmp(const pair<string, int> &A, const pair<string, int> &B)
{
if (A.first == B.first)
return A.second > B.second;
else
return A.first < B.first;
}
/*
set<int>st;
st.insert(1); st.insert(10);
auto it = st.begin();
int x = *it;
auto itt = st.end();
itt--;
int y = *itt;
cout<< x << " " << y << endl;
auto it = st.lower_bound(val);
it--;
int x = *it;
map<int,int>mp;
mp[1] = 1;
mp[2] = 2;
auto it = mp.begin();
int x = it->first;
auto itt = mp.end();
itt--;
int y = itt->first;
cout<< x << " " << y << endl;
// must include this(instead of unordered_map use gp_hash_table)
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
gp_hash_table<int, int> mp;
sort(a+1,a+n+1);array 1 index based sorting
sort(a,a+n);array 0 index based sorting
bs--> l < r-1(l = m or r = m) or l <= r(l = m+1 or r = m+1)
*/
void solve()
{
/*
1. Think Greedy
2. Think Brute Force
3. Think solution in reverse order
4. Think DP [ check constraints carefully ]
5. Check base cases for DP and prove solution for Greedy
6. Think Graph
*/
int n;
cin >> n;
vi vc(n);
fr(vc)cin>>i;
sort(all(vc));
int gc = 0;
fr(vc)
gc = gcd(i,gc);
if(gc == vc[0])
cout << vc[0] << endl;
else
{
int k = gc;
gc = vc[0];
int f = 1;
for(int i = 1;i < n;i++)
{
if(vc[i] == gc)
continue;
if(vc[i]%(vc[i]-gc)==0)
{
f = 0;
break;
}
}
if(f)
cout << gc << endl;
else
{
gc = vc[0]/2;
if(vc[0]%2==0)
gc--;
cout << max(gc,k) << endl;
}
}
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// auto st = clock();
int t = 1;
// cin >> t;
for(int i = 1;i <= t;i++)
{
// cout << "Case " << i << ": ";
solve();
}
// cerr << 1.0*(clock()-st)/CLOCKS_PER_SEC << endl;
return 0;
}
/*Problem_link
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3844kb
input:
3 3 10 7
output:
3
result:
ok 1 number(s): "3"
Test #2:
score: 0
Accepted
time: 9ms
memory: 3968kb
input:
100000 154183567 764881828 59831034 828193326 391773598 487722171 451196811 245718514 750259573 762740115 821999084 28801227 218042831 918898632 881477122 891010192 55732830 509020430 594855913 455478382 456571462 705949609 471532655 550005603 861581472 984465652 883456918 463213251 626620153 371990...
output:
3772
result:
ok 1 number(s): "3772"
Test #3:
score: -100
Wrong Answer
time: 7ms
memory: 4128kb
input:
100000 80 88 53 77 74 63 57 71 75 74 27 14 38 23 24 75 33 89 81 33 100 56 53 77 55 54 63 80 100 15 70 24 100 65 95 22 34 12 31 30 83 20 68 87 23 53 53 55 72 13 57 94 27 94 93 81 96 57 11 81 18 53 34 67 77 65 38 45 45 33 66 47 56 61 60 55 13 61 60 83 24 68 88 50 59 44 27 99 22 82 16 96 62 60 98 48 78...
output:
4
result:
wrong answer 1st numbers differ - expected: '5', found: '4'