列表

详情


NC51091. Sky Code

描述

Stancu likes space travels but he is a poor software developer and will never be able to buy his own spacecraft. That is why he is preparing to steal the spacecraft of Petru. There is only one problem – Petru has locked the spacecraft with a sophisticated cryptosystem based on the ID numbers of the stars from the Milky Way Galaxy. For breaking the system Stancu has to check each subset of four stars such that the only common divisor of their numbers is 1. Nasty, isn’t it? Fortunately, Stancu has succeeded to limit the number of the interesting stars to N but, any way, the possible subsets of four stars can be too many. Help him to find their number and to decide if there is a chance to break the system.

输入描述

n the input file several test cases are given. For each test case on the first line the number N of interesting stars is given . The second line of the test case contains the list of ID numbers of the interesting stars, separated by spaces. Each ID is a positive integer which is no greater than 10000. The input data terminate with the end of file.

输出描述

For each test case the program should print one line with the number of subsets with the asked property.

示例1

输入:

4
2 3 4 5 
4
2 4 6 8 
7
2 3 4 5 7 6 8

输出:

1
0
34

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++ 解法, 执行用时: 40ms, 内存消耗: 740K, 提交时间: 2021-11-06 11:42:37

#include <bits/stdc++.h>
long long n, a[10010], ans[10010], cnt[10010];
int main() {
	for (; scanf("%lld", &n) != EOF; printf("%lld\n", ans[1])) {
		for (int i = 1; i <= n && scanf("%lld", &a[i]); i++)
            for (int j = 1; j * j <= a[i]; j++) 
                if (a[i] % j == 0) cnt[j]++, cnt[a[i] / j] += (a[i] / j != j);;
		for (int i = 10000; i; i--) 
            ans[i] = cnt[i] < 4 ? 0 : cnt[i] * (cnt[i] - 1) * (cnt[i] - 2) * (cnt[i] - 3) / 24, cnt[i] = 0;
		for (int i = 10000; i; i--) 
            for (int j = i + i; j <= 10000; j += i)  ans[i] -= ans[j]; 
	}
	return 0;
}

C++14(g++5.4) 解法, 执行用时: 79ms, 内存消耗: 556K, 提交时间: 2020-10-08 17:17:06

#include <bits/stdc++.h>
long long n, a[10010], ans[10010], cnt[10010];
int main() {
	for (; scanf("%lld", &n) != EOF; printf("%lld\n", ans[1])) {
		for (int i = 1; i <= n && scanf("%lld", &a[i]); i++) for (int j = 1; j * j <= a[i]; j++) if (a[i] % j == 0) cnt[j]++, cnt[a[i] / j] += (a[i] / j != j);;
		for (int i = 10000; i; i--) ans[i] = cnt[i] < 4 ? 0 : cnt[i] * (cnt[i] - 1) * (cnt[i] - 2) * (cnt[i] - 3) / 24, cnt[i] = 0;
		for (int i = 10000; i; i--) for (int j = i + i; j <= 10000; j += i)  ans[i] -= ans[j]; 
	}
	return 0;
}

上一题