zoukankan      html  css  js  c++  java
  • Acwing 1291.轻拍牛头

    题意:

    给定一个序列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 <= num; i ++){
            for(int j = i; j <= num; j += i){
                ans[j] += cnt[i];
            }
        }
    
        for(int i = 0; i < n; i ++){
            printf("%d
    ", 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;
    }
  • 相关阅读:
    springMVC+spring+mybatis整合(包括文件上传和下载)
    mybatis spring 框架整合
    Java AOP 注解配置与xml配置
    Java 实现分页功能
    Arcanist安装使用流程
    Swift 添加KVO
    swift 混编OC instanceType 标识的方法找不到
    Swift UIStackView代码使用
    Swift
    swift 相册PHAssetCollection,PHAsset
  • 原文地址:https://www.cnblogs.com/jungu/p/13383134.html
Copyright © 2011-2022 走看看