NC51091. Sky Code
描述
输入描述
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; }