QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#164877#6883. Noblesse CodeNyansAC ✓740ms30220kbC++141.1kb2023-09-05 14:21:162023-09-05 14:21:16

Judging History

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

  • [2023-09-05 14:21:16]
  • 评测
  • 测评结果:AC
  • 用时:740ms
  • 内存:30220kb
  • [2023-09-05 14:21:16]
  • 提交

answer

#include <cstdio>
#include <algorithm>
struct pr {
	long long a, b, c;
	bool operator < (const pr &rhs) const {
		if (a != rhs.a) return a < rhs.a;
		if (b != rhs.b) return b < rhs.b;
		return c < rhs.c;
	}
}c[5000010];
int T, n, m, q;
int main() {
	scanf("%d", &T);
	while (T--) {
		scanf("%d%d", &n, &q);
		m = 0;
		for (int i = 1; i <= n; ++i) {
			long long a, b;
			scanf("%lld%lld", &a, &b);
			while (a && b) {
				if (a >= b) {
					c[++m] = {a % b, b, a / b};
					a %= b;
				} else {
					c[++m] = {a, b % a, b / a};
					b %= a;
				}
			}
		}
		std::sort(c + 1, c + m + 1);
		for (int i = 1; i <= q; ++i) {
			long long a, b;
			scanf("%lld%lld", &a, &b);
			int ans = 0;
			if (a >= b) {
				pr P = {a % b, b, a / b};
				pr *pos = std::lower_bound(c + 1, c + m + 1, P);
				P.c = 2e18;
				ans = std::lower_bound(c + 1, c + m + 1, P) - pos;
			}
			if (b >= a) {
				pr P = {a, b % a, b / a};
				pr *pos = std::lower_bound(c + 1, c + m + 1, P);
				P.c = 2e18;
				ans += std::lower_bound(c + 1, c + m + 1, P) - pos;
			}
			printf("%d\n", ans);
		}
	}
}

详细

Test #1:

score: 100
Accepted
time: 740ms
memory: 30220kb

input:

54
1000 1000
437949642385872929 860965504436928836
549207725345251140 807757620153191620
595135374173197122 535841165330085586
284216015123638122 991606310035186132
380255965462330700 718440783553992066
600098959546211922 568697564089219926
561489079983301188 490903512830837710
917947220777629314 45...

output:

1
1
1
0
1
1
1
1
1
1
1
1
1
0
1
1
1
0
1
1
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
1
1
1
1
0
1
1
1
0
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
1
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
1
1
1
1
1
0
1
1
1
1
1
1
0
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
...

result:

ok 495000 lines