zoukankan      html  css  js  c++  java
  • Sum

    A square-free integer is an integer which is indivisible by any square number except 11. For example, 6 = 2 cdot 36=23 is square-free, but 12 = 2^2 cdot 312=223 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=16=61=23=32,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 aaand bb are square-free integers. The problem is calculating sum_{i = 1}^nf(i)i=1nf(i).

    Input

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

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

    Output

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

    Hint

    sum_{i = 1}^8 f(i)=f(1)+ cdots +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.

    样例输入

    2
    5
    8

    样例输出

    8
    14

    题目来源

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

     

    欧拉筛法学习。

    代码:

    #include <iostream>
    #define MAX 10000000
    using namespace std;
    int t;
    int n;
    int num[MAX * 2 + 1],sum[MAX * 2 + 1];
    bool vis[MAX * 2 + 1];
    int prime[MAX + 1];
    void init() {
        sum[1] = num[1] = 1;
        for(int i = 2;i <= MAX * 2;i ++) {
            if(!vis[i]) {
                prime[++ prime[0]] = i;
                num[i] = 2;
            }
            for(int j = 1;j <= prime[0] && i * prime[j] <= MAX * 2;j ++) {
                vis[i * prime[j]] = true;
                if(i % prime[j] == 0) {
                    if(i / prime[j] % prime[j]) num[i * prime[j]] = num[i / prime[j]];
                    break;
                }
                num[i * prime[j]] = num[i] * num[prime[j]];
            }
            sum[i] += sum[i - 1] + num[i];
        }
    }
    int main() {
        scanf("%d",&t);
        init();
        while(t --) {
            scanf("%d",&n);
            printf("%d
    ",sum[n]);
        }
        return 0;
    }
  • 相关阅读:
    VS注释提示英文变中文的方法
    Windows 10安裝.net Framework 3.5出現0X800F0954錯誤
    NodeJS+NPM+Bower+Android环境安装配置
    复合索引
    高并发的核心技术-幂等的实现方案
    Redis初使用
    数据库SQL查找包含某列的所有table
    多线程中的wait与sleep到底谁释放了锁
    https配置
    iOS下的实际网络连接状态检测(转)
  • 原文地址:https://www.cnblogs.com/8023spz/p/10757525.html
Copyright © 2011-2022 走看看