(南京区域资格赛)J-Sum

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/OscaronMar/article/details/82290452

A square-free integer is an integer which is indivisible by any square number except 1. For example, 6 = 2·3 is square-free, but 12 = 22·3 is not, because 22
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 = 1·6=6·1=2·3=3·2, n=ab and n=ba are considered different if a≠b. f(n) is the number of decomposition ways that n=ab such that a and b are square-free integers. The problem is calculating这里写图片描述
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(n≤2·10^7).
Output
For each test case, print the answer这里写图片描述
这里写图片描述
Sample Input
2
5
8
Sample Output
8
14
题目链接:https://nanti.jisuanke.com/t/30999

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int arr[20000010];
int main()
{
    int n=2e7+1;
    for(int i=2; i*i<=n; i++){
        int t=i*i;
        for(int j=t; j<=n; j+=t)
        {
            arr[j]=1;
        }
    }
    for(int i=1;i<=n;i++){
        arr[i]+=arr[i-1];
    }
    int t;
    scanf("%d",&t);
    long long ans = 0;
    for(int i = 0; i < t; i++){
        scanf("%d",&n);
        ans=0;
        for(int i=1; i<=n; ){
            int j=n/(n/i)+1;
            ans+=(j-i)*(long long)(n/i);
            i=j;
        }
        for(int i=1; i<=n;){
            int j=n/(n/i)+1;
            ans-=2*(arr[j-1]-arr[i-1])*(long long)(n/i);
            i=j;
        }
        for(int i=1; i<=n;){
            int j=n/(n/i)+1;
            ans+=(arr[j-1]-arr[i-1])*(long long)arr[n/i];
            i=j;
        }
        printf("%lld\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/OscaronMar/article/details/82290452
今日推荐