Sum of the Line UPC5222 (容斥)

莫过一日曝十日寒。

5222: Sum of the Line

时间限制: 1 Sec  内存限制: 128 MB
提交: 212  解决: 61
[提交] [状态] [讨论版] [命题人:admin]

题目描述

Consider a triangle of integers, denoted by T. The value at (r, c) is denoted by Tr,c , where 1 ≤ r and 1 ≤ c ≤ r. If the greatest common divisor of r and c is exactly 1, Tr,c = c, or 0 otherwise.
Now, we have another triangle of integers, denoted by S. The value at (r, c) is denoted by S r,c , where 1 ≤ r and 1 ≤ c ≤ r. S r,c is defined as the summation    
Here comes your turn. For given positive integer k, you need to calculate the summation of elements in k-th row of the triangle S.

 

输入

The first line of input contains an integer t (1 ≤ t ≤ 10000) which is the number of test cases.
Each test case includes a single line with an integer k described as above satisfying 2 ≤ k ≤ 10^8 .

 

输出

For each case, calculate the summation of elements in the k-th row of S, and output the remainder when it divided
by 998244353.

 

样例输入

2
2
3

 

样例输出

1
5

 

来源/分类

ICPC2017  Urumqi 

 

小结:不到最后一刻,不放弃。

 

容斥 先假设所有的T(r,i)都等于i,全部加起来会发现是1^2+2^2+3^2+4^2+.........n^2,求和公式为n*(n+1)*(2*n+1)/6。

然后对k进行唯一分解,会发现最多不到10个素数,在2^10内容斥就好了。

 

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 998244353;
int vis[15]={0},cnt;
/*const int N=1e8+10;
int prime[N],x[N],cnt;*/
/*void is_prime(){
    for(int i=2;i<=N;i++){
        if(x[i]==0){
            x[i]=1;
            prime[cnt++]=i;
            for(int j=i*2;j<=N;j+=i){
                x[j]=2;
            }
        }
    }
}*/
 
ll qpow(ll a,ll b){
    ll ans=1;
    while(b){
        if(b&1){
            ans=(ans*a)%mod;
        }
        a=(a*a)%mod;
        b>>=1;
    }
    return ans%mod;
}
void solve(){
    ll n;
    scanf("%lld",&n);
    ll nn = n;
    ll ans =n%mod*(n+1)%mod*(2*n+1)%mod*qpow(6,mod-2)%mod;
    
    int b[100];
    cnt=0;
    //唯一分解定理
    for(int i=2;i*i<=n;i++){
        if(n%i==0){
            b[cnt++]=i;
            while(n%i==0){
                n/=i;
            }
        }
    }
    if(n!=1){
        b[cnt++]=n;
    }
    /*for(int i=0;i<cnt;i++){
        printf("%d%c",b[i],i==cnt-1?'\n':' ');
    }*/
    
    for(ll i = 1; i < (1<<cnt); i++)
    {
        ll cnt1 = 0, tmp = 1;
        for(int j=0; j<cnt; j++)
        {
            if(i & (1LL<<j))
            {
                cnt1++;
                tmp *= b[j];
                tmp %= mod;
            }
        }
        ll nnn = nn/tmp;
        if(cnt1 & 1)
            ans = (ans-tmp*tmp%mod*nnn%mod*(nnn+1)%mod*(2*nnn+1)%mod*qpow(6,mod-2)%mod + mod)%mod;
        else
            ans = (ans+tmp*tmp%mod*nnn%mod*(nnn+1)%mod*(2*nnn+1)%mod*qpow(6,mod-2)%mod)%mod;
    }
    printf("%lld\n",ans);
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        solve();
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值