ACM-ICPC 2017 Asia Urumqi K. Sum of the Line 容斥

5222: Sum of the Line

时间限制: 1 Sec  内存限制: 128 MB
提交: 203  解决: 59
[提交] [状态] [讨论版] [命题人: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

[提交] [状态]

1e8的数据,那么k的合数会有很多,直接算肯定会超时

考虑把k质因数分解

质因数p的倍数一定和k不互质,所以p的倍数一定需要减去

但是质因数的倍数可能重复,需要运用容斥的思想,奇减偶加

用二进制的方法判断每个质因数取与不取

代码:

#include <bits/stdc++.h>
using namespace std;
const  int maxn=1e4+100;
#define INF 0x3f3f3f;
const int mod=998244353;
typedef  long long ll;
bool prime[maxn];
int a[maxn],k=0;
ll inv6;
void qpow() //快速幂
{
    ll a = 6;
    int b = mod - 2;
    ll ret = 1;
    while(b){
        if(b & 1)  ret = ret * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    inv6 = ret;
}
void init() //素数筛
{
    memset(prime,false,sizeof(prime));
    prime[1]=true;
    for(int i=2; i<maxn; i++)
    {
        if(prime[i]==false)
        {
            a[k++]=i;
        }
        for(int j=0; j<k && a[j]*i<maxn; j++)
        {
            prime[a[j] * i]=true;
            if(i % a[j]==0)   break;
        }
    }
    return ;
}

vector<int> p;
void div(ll n) //质因数分解
{
    for(int i=0; i<k && n>1; i++){
        if(n % a[i] == 0)
            p.push_back(a[i]);
        while(n % a[i] == 0)
            n /= a[i];
    }
    if(n>1)
        p.push_back(n);
}

ll c(ll x) //计算1^2+2^2……+x^2 的公式
{
    ll sum = (x * (x + 1) % mod * (2 * x + 1) % mod) % mod;
    return sum * inv6 % mod;
}

int main()
{
    qpow();
    init();
    int t;
    scanf("%d",&t);
    while(t--){
        ll n;
        scanf("%lld",&n);
        p.clear();
        div(n);
        ll ans = c(n);
        for(int i=1; i< (1 << (p.size())); i++){
            ll x = 1;
            int cnt = 0;
            for(int j=0; j< (p.size()); j++){
                if((i >> j) & 1){
                    x = x * p[j] %mod;
                    cnt++;
                }
            }
            if(cnt & 1)   //奇减偶加,容斥基本思想
                ans = (ans - (x * x % mod) * c(n / x) % mod + mod) % mod;
            else
                ans = (ans +  (x * x % mod) * c(n / x) % mod) % mod;
        }
        printf("%lld\n",ans%mod);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/renxiaomiao/p/9642659.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值