标签:const lse scanf 暴力 out define stream 其他 max
题意:
给定一个序列ai,问序列中其他数中有多少个数是它的约数
思路:
暴力求法会超时。O(n²)
最优解:先储存每个数的个数,遍历x,每个x的倍数加上x的个数
注:最后每个数的答案要-1(减去本身)
Code:
#pragma GCC optimize(3) #pragma GCC optimize(2) #include <map> #include <set> #include <array> #include <queue> #include <stack> #include <vector> #include <cstdio> #include <cstring> #include <sstream> #include <iostream> #include <stdlib.h> #include <algorithm> #include <unordered_map> using namespace std; typedef long long ll; typedef pair<int, int> PII; #define Time (double)clock() / CLOCKS_PER_SEC #define sd(a) scanf("%d", &a) #define sdd(a, b) scanf("%d%d", &a, &b) #define slld(a) scanf("%lld", &a) #define slldd(a, b) scanf("%lld%lld", &a, &b) const int N = 1e5 + 20; const int M = 1e6 + 20; const int mod = 1e9 + 7; int n; int a[N]; int cnt[M], ans[M]; void solve() { sd(n); int num; for(int i = 0; i < n; i ++){ sd(a[i]); cnt[a[i]] ++; num = max(num, a[i]); } for(int i = 1; i < n; i ++){ for(int j = i; j <= num; j += i){ ans[j] += cnt[i]; } } for(int i = 0; i < n; i ++){ printf("%d\n", ans[a[i]] - 1); } } int main() { #ifdef ONLINE_JUDGE #else freopen("/home/jungu/code/in.txt", "r", stdin); // freopen("/home/jungu/code/out.txt", "w", stdout); // freopen("/home/jungu/code/out.txt","w",stdout); #endif // ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int T = 1; // sd(T); while(T --){ solve(); } return 0; }
标签:const lse scanf 暴力 out define stream 其他 max
原文地址:https://www.cnblogs.com/jungu/p/13383134.html