2019HDU多校赛第九场D、Rikka with Geometric Sequence(杜教筛+数论分块)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/C20180602_csq/article/details/100188811

Rikka with Geometric Sequence

Time Limit: 14000/7000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 130    Accepted Submission(s): 53


 

Problem Description

A long time ago, Rikka was not good at math. Worrying about Rikka's grades, Yuta sets many interesting math problems for Rikka to help her improve her skills.

Now, as Rikka makes more and more progress on math, more and more she feels the joy of solving math tasks. Today, Yuta is quite busy and has no time to seek for new problems for Rikka. Therefore, for the first time, Rikka tries to come up with a problem herself.

Setting a problem is just like building blocks. The first step is to choose the bricks. Rikka selects the concepts of "geometric sequence" and "subsequence":

Sequence a1,…,ak is a geometric sequence if and only if for each index i∈[2,n−1], the values in the sequence holds a2i=ai−1×ai+1.

Sequnce b1,…,bt is a subsequence of a1,…,ak if and only if there exists an index sequence c1,…,ct(1≤ci≤k) which satisfies ci<ci+1 for each i∈[1,n)and aci=bi for each i∈[1,n].

The second step is to combine the bricks. It is quite simple for Rikka: she soon finds an interesting problem:

Given a positive integer n, count the number of different geometric subsequences of 1,2,…,n.

The last step, and also the most important step, is to solve the problem. However, this task seems to be too difficult for Rikka. Therefore she seeks for help from you: Could you please help her solve this interesting math problem?

Input

The first line of the input contains a single integer T(1≤T≤1000).

For each test case, the input contains a single line with a single integer n(1≤n≤5×1017).

The input guarantees that there are no more than 3 test cases with n>109.

Output

For each test case, output a single line with a single integer, the answer. The answer can be very large, you only need to print the answer modulo 998244353.

Hint

When n=4, the valid subsequences are {1},{2},{3},{4},{1,2},{1,3},{1,4},{2,3},{2,4},{3,4},{1,2,4}. Therefore the answer is 11.

Sample Input

10

1

2

3

4

5

6

7

8

9

100

Sample Output

1

3

6

11

16

22

29

39

50

5187

题意

求数列1、2、3、4......n的子序列是等比数列的个数。

题解

设等比数列的长度为k,则k=1的答案是n,k=2的答案是n*(n-1)/2

当k>3时,我们设该等比数列的公比为a/b  (a>b&&gcd(a,b)=1),则我们可以推出该等比数列的末项被a的k-1次方整除

于是我们可以直接枚举a的值,对于每一个a(a^(k-1)<=n)我们可以求出所有满足条件的b(就是phi(a))

然后满足条件的a的个数就是floor(n/(a^(k-1)))

两个乘起来就好啦

难点在于k=3的时候,我们发现它的a的取值有1e9种,无法暴力算

所以我们就可以用杜教筛+数论分块来做

代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
#define LL long long
const int N=10000000;
const int mod=998244353;
const int ni=499122177;
int prime[N+5],tot;
bool vis[N+5];
LL phi[N+5],tmpphi[N+5];
void shai()
{
    vis[1]=1;tot=0;tmpphi[1]=phi[1]=1;
    int i,j;
    for(i=2;i<=N;i++){
        if(!vis[i]){
            prime[++tot]=i;
            phi[i]=1ll*i-1ll;
        }
        for(j=1;j<=tot;j++){
            LL tmp=1ll*i*prime[j];
            if(tmp>N) break;
            vis[tmp]=1;
            if(i%prime[j]==0){
                phi[tmp]=1ll*phi[i]*prime[j];
                break;
            }
            else
                phi[tmp]=1ll*phi[i]*(prime[j]-1);
        }
    }
    for(int i=2;i<=N;i++){
        tmpphi[i]=phi[i];
        phi[i]=(1ll*phi[i]+1ll*phi[i-1])%mod;
    }
}
map<LL,LL> Phi;
LL DJshai(LL n)
{
    if(n<=N) return phi[n];
    if(Phi.count(n)) return Phi[n];
    LL sum=0;
    for(LL nxt,i=2;i<=n;i=1ll*nxt+1ll){
        nxt=n/(n/i);
        sum=(1ll*sum+1ll*(nxt-i+1ll)%mod*DJshai(n/i)%mod)%mod;
    }
    return Phi[n]=((1ll*n%mod*(n%mod+1)/2%mod-1ll*sum)%mod+1ll*mod)%mod;
}
int main()
{
    LL n,ans;
    shai();
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%lld",&n);
        ans=(n%mod+n%mod*(n%mod-1)/2)%mod;
        for(LL nxt,i=2;i*i<=n;i=nxt+1ll){//k=3
            nxt=floor(sqrt(n/(n/(i*i))));
            ans=(1ll*ans+1ll*(DJshai(nxt)-DJshai(i-1))%mod*(n/(i*i)%mod))%mod;
        }
        LL lim=floor(pow(n,1.0/3));
        for(LL i=2;i<=lim;i++){//k>3
            for(LL j=1ll*i*i*i;;j*=i){
                ans=(ans+tmpphi[i]*(n/j%mod))%mod;
                if(j>n/i)break;
            }
        }
        printf("%lld\n",(ans+1ll*mod)%mod);
    }
}

猜你喜欢

转载自blog.csdn.net/C20180602_csq/article/details/100188811