zoukankan      html  css  js  c++  java
  • 南京网络赛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 = 2^2 cdot 312=22⋅3 is not, because 2^222 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 = 1cdot 6=6 cdot 1=2cdot 3=3cdot 2, n=ab6=1⋅6=6⋅1=2⋅3=3⋅2,n=ab and n=ban=ba are considered different if a ot = ba̸=b. f(n)f(n) is the number of decomposition ways that n=abn=ab such that aa and bb are square-free integers. The problem is calculating sum_{i = 1}^nf(i)∑i=1n​f(i).

    Input

    The first line contains an integer T(Tle 20)T(T≤20), denoting the number of test cases.

    For each test case, there first line has a integer n(n le 2cdot 10^7)n(n≤2⋅107).

    Output

    For each test case, print the answer sum_{i = 1}^n f(i)∑i=1n​f(i).

    Hint

    sum_{i = 1}^8 f(i)=f(1)+ cdots +f(8)∑i=18​f(i)=f(1)+⋯+f(8)
    =1+2+2+1+2+4+2+0=14=1+2+2+1+2+4+2+0=14.

    样例输入复制

    2
    5
    8

    样例输出复制

    8
    14

    题目来源

    ACM-ICPC 2018 南京赛区网络预赛

    用素数筛类似的方法先把f的表打出来

    一个数可以分解成 n = p1^a1 * p2^a2 * p3^a3.......其中p1,p2,p3...都是素数

    如果a1,a2,a3...中有一个大于2,那么f(n)=0因为不管怎样组合都会使一个因子是平方数

    如果a1,a2,a3...中有m个是1,剩下的都是2 那么只有那些是2的就必须分开,剩下的每一个数都有2中可能所以f(n)=2^m

    打表 prime[p] = p prime[i*p]=1

    对于比i小的所有素数 i是作为他们的倍数进行处理的

    如果i%prime[j]==0 而且i%(prime[j]*prime[j])==0那么i*prime[j]肯定指数有一个大于2了 f[i*prime[j]] = 0

    如果i%prime[j]==0 那么i*prime[j]要少掉一个数可以选择 所以f[i*prime[j]] = f[i] / 2

    如果上面一个都不是 那么i*prime[j]之后就多了一个数可以选择 所以f[i*prime[j]] = f[i] * 2

    不加if(i%prime[j]==0)break;会T,还没想懂为什么....

    
    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #include<stack>
    #include<queue>
    #include<map>
    #include<vector>
    #include<set>
    //#include<bits/stdc++.h>
    #define inf 0x3f3f3f3f
    using namespace std;
    
    typedef long long LL;
    
    
    int t, n, cnt;
    const int maxn = 2e7 + 5;
    int f[maxn], prime[maxn];
    
    void table()
    {
        memset(f, 0, sizeof(f));
        memset(prime, 0, sizeof(prime));
        cnt = 0;
        f[1] = 1;
        for(int i = 2; i < maxn; i++){
            if(!prime[i]){
                prime[cnt++] = i;
                f[i] = 2;
            }
            for(int j = 0; j < cnt && prime[j] * i < maxn; j++){
                prime[prime[j] * i] = 1;
                if(i % prime[j] == 0){
                    if(i % (prime[j] * prime[j]) == 0){
                        f[i * prime[j]] = 0;
                    }
                    else{
                        f[i * prime[j]] = f[i] / 2;
                    }
                }
                else{
                    f[i * prime[j]] = f[i] * 2;
                }
                if(i % prime[j] == 0)break;
            }
        }
    }
    
    
    int main()
    {
        cin>>t;
        table();
        while(t--){
            scanf("%d", &n);
            LL ans = 0;
            for(int i = 1; i <= n; i++){
                ans += f[i];
            }
            printf("%lld
    ", ans);
        }
    	return 0;
    }
    
  • 相关阅读:
    什么是架构
    jenkins权限插件配置
    解决Error response from daemon: oci runtime error: container_linux.go:247: starting container process
    解决“/tmp/crontab bad minute”问题
    yml链接mysql路径serverTimezone=UTC的那些坑
    java请求头导致特殊字符为空问题
    使用ssh连接WSL
    系统设计与任务分配
    需求规格说明书
    选题报告
  • 原文地址:https://www.cnblogs.com/wyboooo/p/9643374.html
Copyright © 2011-2022 走看看