QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#64779 | #4624. Shortest Path in GCD Graph | qinjianbin# | AC ✓ | 696ms | 85620kb | C++17 | 1.4kb | 2022-11-25 15:46:42 | 2022-11-25 15:46:44 |
Judging History
answer
#include<bits/stdc++.h>
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define _for(i, a, b) for(int i = (a); i <= (b); i++)
using namespace std;
typedef long long ll;
const int N = 1e7 + 10;
int vis[N], a[N], n, q;
vector<int> p, ve;
ll ans;
void init()
{
vis[0] = vis[1] = 1;
_for(i, 2, 1e7)
{
if(!vis[i]) a[i] = i, p.push_back(i);
for(int x: p)
{
if(x * i > 1e7) break;
vis[x * i] = 1;
a[x * i] = x;
if(i % x == 0) break;
}
}
}
vector<int> deal(int x)
{
vector<int> res;
while(x > 1)
{
int t = a[x];
res.push_back(t);
while(x % t == 0) x /= t;
}
return res;
}
void dfs(int i, int cnt, ll cur)
{
if(i == ve.size())
{
if(cur > 1)
{
if(cnt % 2 == 1) ans += n / cur;
else ans -= n / cur;
}
return;
}
dfs(i + 1, cnt + 1, cur * ve[i]);
dfs(i + 1, cnt, cur);
}
int gcd(int a, int b) { return !b ? a: gcd(b, a % b); }
int main()
{
init();
scanf("%d%d", &n, &q);
while(q--)
{
int u, v;
scanf("%d%d", &u, &v);
vector<int> pu = deal(u), pv = deal(v);
ve = pu;
for(int x: pv) ve.push_back(x);
sort(ve.begin(), ve.end());
ve.erase(unique(ve.begin(), ve.end()), ve.end());
if(ve.size() == pu.size() + pv.size())
{
puts("1 1");
continue;
}
ans = 0;
dfs(1, 1, ve[0]);
dfs(1, 0, 1);
printf("%d %lld\n", 2, n - ans + (gcd(u, v) == 2));
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 696ms
memory: 85620kb
input:
10000000 50000 6063825 1688077 9748275 1717197 5067284 9994733 8895125 6988260 9446181 5378152 9460161 1794337 3393611 9297610 7525386 5806968 9608609 2225468 9808741 2596273 606784 8423027 2361246 4662937 4385583 1195254 8440346 4819524 4256844 679638 8959165 3868431 9063859 4480534 9568261 1389414...
output:
1 1 2 5240548 1 1 2 2666605 1 1 1 1 1 1 2 2903766 1 1 1 1 1 1 1 1 2 3216618 2 3333325 2 3311989 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2637091 1 1 1 1 1 1 2 3589273 1 1 2 2178004 1 1 1 1 1 1 2 3075313 2 4571237 2 3392155 2 3999993 1 1 2 3144296 1 1 1 1 2 2840582 1 1 2 5121187 2 2461856 1 1 1 1 1 1 1 1 1 ...
result:
ok 50000 lines