zoukankan      html  css  js  c++  java
  • ACM-ICPC 2018 南京赛区网络预赛 J.Sum [ 类打表 ]

    A square-free integer is an integer which is indivisible by any square number except 11. For example, 6 = 2 cdot 36=2⋅3 is square-free, but 12=22312=22∗3 is not, because 2222
    is a square number. Some integers could be decomposed into product of two square-free integers, there may be more than one decomposition ways. For example, 6=16=61=23=32,n=ab,n=ba6=1∗6=6∗1=2∗3=3∗2,n=ab,n=ba, are considered different if aba≠b. f(n) is the number of decomposition ways that n=ab such that a and b are square-free integers. The problem is calculatingni=1f(i)∑i=1nf(i)

    Input
    The first line contains an integer T(T≤20), denoting the number of test cases.
    For each test case, there first line has a integer n(n2107)n(n≤2⋅107)

    Output
    For each test case, print the answer ni=1f(i)∑i=1nf(i)

    Hint
    8i=1f(i)=f(1)++f(8)∑i=18f(i)=f(1)+⋯+f(8)=1+2+2+1+2+4+2+0=14=1+2+2+1+2+4+2+0=14.

    题意:找出一个数的所有乘数组合,使这些乘数组合中的任一因数都不存在完全平方因数。求从1到n的的所有数的乘数组合之和。

    思路:
    打个表找到每一个数的最小的质因数 ,然后
    if ( i % (mprim[i] * mprim[i] * mprim[i]) == 0 ) ans[i] = 0;
    else if ( i % (mprim[i] * mprim[i]) == 0 ) ans[i] = ans[i / (mprim[i]*mprim[i])];
    else ans[i] = ans[i / mprim[i]] * 2;

    AC code:

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    typedef long long ll;
    typedef pair<int,int>pii;
    
    const int maxn = 2e7+50;
    
    int p[maxn] ,mprim[maxn] ,tot ;
    bool vis[maxn];
    ll ans[maxn];
    
    void init() {
        tot = 0;
        memset(vis ,true ,sizeof(vis) );
        for (int i = 2;i < maxn;i++) {
            if ( vis[i] ) p[tot++] = i,mprim[i] = i;
            for (int j = 0; j < tot && i * p[j]< maxn ;j++) {
                vis[i * p[j]] = false;
                mprim[i * p[j]] = p[j];
                if ( i % p[j] == 0 ) break;
            }
        }
        ans[1] = 1;
        for (int i = 2;i < maxn ;i++) {
            if ( i % (mprim[i] * mprim[i] * mprim[i]) == 0 ) ans[i] = 0;
            else if ( i % (mprim[i] * mprim[i]) == 0 ) ans[i] = ans[i / (mprim[i]*mprim[i])];
            else ans[i] = ans[i / mprim[i]] * 2;
        }
    }
    
    int main() {
        init();
        int t ,n ; cin>>t;
        for (int i = 1;i <= t;i++) {
            scanf("%d",&n);
            ll res = 0;
            for (int i = 1;i<=n;i++) res += ans[i];
            printf("%lld
    ",res);
        }
        return 0;
    }
  • 相关阅读:
    七 、linux正则表达式
    六、通配符
    Codeforces1099D.Sum in the tree(贪心)
    叮,出现!
    Codeforces1056E.Check Transcription(枚举+Hash)
    2018.11.25 AMC-ICPC 亚洲区域赛(焦作站)吊银
    Gym101889J. Jumping frog(合数分解+环形dp预处理)
    Gym101889E. Enigma(bfs+数位)
    Gym101889B. Buggy ICPC(打表)
    Codeforces1076F. Summer Practice Report(贪心+动态规划)
  • 原文地址:https://www.cnblogs.com/Nlifea/p/11745949.html
Copyright © 2011-2022 走看看