QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#64771#4624. Shortest Path in GCD Graphqinjianbin#WA 671ms87152kbC++171.3kb2022-11-25 15:38:162022-11-25 15:38:18

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-11-25 15:38:18]
  • 评测
  • 测评结果:WA
  • 用时:671ms
  • 内存:87152kb
  • [2022-11-25 15:38:16]
  • 提交

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 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);
 	}
	
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 671ms
memory: 87152kb

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 3333324
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 3589272
1 1
2 2178004
1 1
1 1
1 1
2 3075313
2 4571237
2 3392154
2 3999992
1 1
2 3144295
1 1
1 1
2 2840582
1 1
2 5121187
2 2461856
1 1
1 1
1 1
1 1
1 ...

result:

wrong answer 14th lines differ - expected: '2 3333325', found: '2 3333324'