2018 ACM-ICPC南京赛区网络预赛 J.Sum(线性筛+思维)

题目链接

题目描述

A square-free integer is an integer which is indivisible by any square number except 1 1 1. For example, 6 = 2 ⋅ 3 6 = 2 \cdot 3 6=23 is square-free, but 12 = 2 2 ⋅ 3 12 = 2^2 \cdot 3 12=223 is not, because 2 2 2^2 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 = 2 ⋅ 3 = 3 ⋅ 2 6 = 1\cdot 6=2\cdot 3=3\cdot 2 6=16=23=32, n = a b n=ab n=ab and n = b a n=ba n=ba are considered different if a ≠ b a \neq b a̸=b . f ( n ) f(n) f(n) is the number of decomposition ways that n = a b n=ab n=ab such that a a a and b b b are square-free integers. The problem is calculating ∑ i = 1 n f ( i ) \sum_{i = 1}^nf(i) i=1nf(i).

Input

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

For each test case, there first line has a integer n ( n ≤ 2 ⋅ 1 0 7 ) n(n \leq 2\cdot10^7) n(n2107).

Output

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

Hint

∑ i − 1 8 f ( i ) = f ( 1 ) + f ( 2 ) + ⋯ + f ( 8 ) \sum_{i-1}^8f(i)=f(1)+f(2)+\cdots +f(8) i18f(i)=f(1)+f(2)++f(8)

= 1 + 2 + 2 + 1 + 2 + 4 + 2 + 0 = 14. =1+2+2+1+2+4+2+0=14. =1+2+2+1+2+4+2+0=14.

样例输入
2
5
8
样例输出
8
14

题目大意

将一个整数 m m m分解为两个整数的乘积 a ⋅ b a\cdot b ab,并且满足 a , b a,b a,b不能被任意一个平方数整除,求整数 n n n的划分种数。

解题思路

读完题目就应该想到整数的划分,根据整数唯一划分原则,任何一个整数都可以分解为其质因子的乘积,并且形式唯一,即 m = p 1 a ⋅ p 2 b ⋯ p n z m=p_1^a\cdot p_2^b\cdots p_n^z m=p1ap2bpnz。如果分解结果中存在某一个指数 ≥ 3 \geq 3 3,那么这个数 m m m一定不是无方形整数,反之,分解方式的个数为 2 k 2^k 2k(其中 k k k为指数中为 1 1 1的个数)。思路是这样,但是这道题对每一个整数都进行质因子分解时时间复杂度为 O ( n s q r t ( n ) ) O(nsqrt(n)) O(nsqrt(n)),会 t l e tle tle,所以要用到欧拉筛(线性筛)。

  • p p p为素数,那么 f ( p ) = 2 f(p)=2 f(p)=2

  • p p p% p r i m [ j ] = = 0 prim[j]==0 prim[j]==0 p p p% ( p r i m [ j ] × p r i m [ j ] ) = = 0 (prim[j]\times prim[j])==0 (prim[j]×prim[j])==0,说明 p × p r i m [ j ] p\times prim[j] p×prim[j]中有 p r i m [ j ] prim[j] prim[j]的指数为 3 3 3,那么 f ( p × p r i m [ j ] ) = 0 f(p\times prim[j])=0 f(p×prim[j])=0,若 p p p% p r i m [ j ] = = 0 prim[j]==0 prim[j]==0,此时说明 p p p × p r i m [ j ] \times prim[j] ×prim[j] p r i m [ j ] prim[j] prim[j]的指数为 2 2 2,那么 p r i m [ j ] prim[j] prim[j]的加入对 p × p r i m [ j ] p\times prim[j] p×prim[j]的分解方式没有影响,即 f [ p × p r i m [ j ] ] = f [ p / p r i m [ j ] ] f[p\times prim[j]]=f[p/prim[j]] f[p×prim[j]]=f[p/prim[j]]或者 f [ p × p r i m [ j ] ] = f [ p ] / 2 f[p\times prim[j]]=f[p]/2 f[p×prim[j]]=f[p]/2

  • 否则, f [ p × p r i m [ j ] ] = f [ p ] × 2 f[p\times prim[j]]=f[p]\times 2 f[p×prim[j]]=f[p]×2

用欧拉筛的巧处是每一个点只会被计算一次,同时在计算 p × p r i m [ j ] p\times prim[j] p×prim[j]中,不需要去关心 j j j是怎样的一个数, p r i m [ j ] prim[j] prim[j]中的值到底是多少。

AC代码

#include 
#define INF 0x3f3f3f3f3f3f3f
#define mod 10000019
using namespace std;
const int Max_N=2e7+10;
typedef pair<int,int>P;
typedef long long ll;
typedef unsigned long long ull;
bool is_prime[Max_N];
ll f[Max_N];
ll sum[Max_N];
int prime[Max_N];
void getprim()
{
    f[1]=1;
    for(int i=0;i<Max_N;i++) is_prime[i]=true;
    is_prime[0]=is_prime[1]=false;
    int cnt=0;
    for(int i=2;i<Max_N;i++)
    {
        if(is_prime[i])
        {
            prime[cnt++]=i;
            f[i]=2;
        }
        for(int j=0;j<cnt&&i*prime[j]<Max_N;j++)
        {
            is_prime[i*prime[j]]=false;
            if(i%prime[j]==0)
            {
                if(i%(prime[j]*prime[j])==0)f[i*prime[j]]=0;
                else f[i*prime[j]]=f[i/prime[j]];
                break;
            }
            f[i*prime[j]]=2*f[i];
        }
    }
}
int main(int argc, char const *argv[])
{
    int t;
    cin>>t;
    getprim();
    for(int i=1;i<Max_N;i++)sum[i]=sum[i-1]+f[i];
    while(t--)
    {
        int n;
        cin>>n;
        cout<<sum[n]<<endl;
    }
    return 0;
}

总结

如果解题需要用到整数的唯一划分原则的话,很大可能会用到欧拉筛。要学会将欧拉筛灵活的应用。

你可能感兴趣的:(数论)