# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
342658 | VodkaInTheJar | Sažetak (COCI17_sazetak) | C++14 | 1085 ms | 492 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define endl '\n'
using namespace std;
const int maxn = 5e6 + 3;
const int maxm = 13;
long long n, m;
long long k[maxm];
void read()
{
cin >> n >> m;
for (int i = 1; i <= m; i++)
cin >> k[i];
}
long long gcd(long long a, long long b)
{
while (b)
{
long long r = a % b;
a = b;
b = r;
}
return a;
}
long long lcm(long long a, long long b)
{
return a * b / gcd(a, b);
}
pair <long long, long long> extended_euclid(long long a, long long b)
{
if (b == 0)
return {1, 0};
long long r = a % b, q = a / b;
auto res = extended_euclid(b, r);
return {res.second, res.first - 1ll * res.second * q};
}
vector <pair <long long, long long> > v;
long long ans;
void dfs(int pos, int cnt_bits, long long r, long long period)
{
if (r > n-2)
return;
if (pos == (int)v.size())
{
if (cnt_bits == 0)
return;
if (cnt_bits & 1)
ans += 1 + (n-2-r) / period;
else
ans -= 1 + (n-2-r) / period;
return;
}
dfs(pos + 1, cnt_bits, r, period);
long long t = r - v[pos].second;
long long d = gcd(period, v[pos].first);
if (t % d != 0)
return;
t /= d;
auto res = extended_euclid(v[pos].first / d, period / d);
long long num = res.first * t * v[pos].first + v[pos].second;
long long nxt_period = lcm(period, v[pos].first);
num %= nxt_period;
if (num < 0)
num += nxt_period;
dfs(pos + 1, cnt_bits + 1, num, nxt_period);
}
void solve()
{
for (int i = 1; i <= m; i++)
if ((n - 1) % k[i] == 0)
{
ans++;
break;
}
n++;
for (int i = 1; i <= m; i++)
for (int j = 1; j <= m; j++)
if (gcd(k[i], k[j]) == 1)
{
long long period = 1ll * k[i] * k[j];
auto sol = extended_euclid(k[j], k[i]);
long long num = sol.second * k[i];
num %= period;
if (num < 0)
num += period;
if (num > n-2)
continue;
v.push_back({period, num});
}
if (v.empty())
{
cout << ans << endl;
return;
}
dfs(0, 0, 0, 1);
cout << ans << endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
read();
solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |