Sum of the Line(容斥原理)

                                        Sum of the Line

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

                                                                            [提交]             [状态]

题意:

给你个T表的规则,就是对于1 ≤ r and 1 ≤ c ≤ r. 如果gcd(r,c)== 1, Tr,c = c, 否则等于0。S表就是对于某一行来说求当前点到最后的一个对于T表的后缀和。然后给你个K求出来K行的一个S表的和。

题解:

先令 T 中所有位置的值为相应的c,那么答案为 1^2+2^2+...+k^2。下面考虑删去 T(r,c)=0 的位置造成的代价,也就是计算不和k互质的数对答案的贡献。

略加思考就可以得到一个结论,删除第c个数(值肯定也为c)花费的代价一定是c^2

暴力直接求的话会发现有的k的合数有上万个,铁定超时。

考虑对k进行唯一分解,发现k分解后的素数最多也就九个,可以在2^9内用容斥原理进行求解,设m=p1^a1*p2^a2*...*pu^au

那么所有与k不互质的数的贡献就是p1的倍数的贡献+p2的倍数的贡献+...+pu的倍数的贡献-p1*p2的倍数的贡献-p1*p3的倍数的贡献-...+p1*p2*p3的倍数的贡献+......

以p1的倍数的贡献为例,他的贡献是(p1*1)^2+(p1*2)^2+...+(p1*[k/p1])^2,然后使用公式1^2+2^2+...+n^2=n*(n+1)*(2*n+1)/6就可以做了

容斥 : 当元素个数为奇数时为加,当元素个数为偶数是为减

公式证明

PS 比赛的时候和队友想到了这个思路,但是卡在了这个减去T=0的情况上面,当时队友也想到了用容斥来做,但是感觉麻烦就没再继续想,这个题用二进制枚举了容斥的所有情况,想不到啊,,,,还不是因为菜。

最后 要注意的是mod这个问题,死了好几次了,优先级的判断也需要注意,相减的话一定要加上mod,刚开始最后不输出结果,就是最后结果忘了加上mod

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int mod = 998244353;
const int maxn=1e4+10;
ll k;
ll n;
ll prime[maxn];
ll isprime[maxn];
ll prime_cnt;
ll p[20];
ll p_cnt;

ll qpow(ll a,ll b)
{
    ll ans=1;
    while(b)
    {
        if(b%2)
            ans=(ans%mod*a%mod)%mod;
        a=(a%mod*a%mod)%mod;
        b/=2;
    }
    return ans%mod;
}
ll six=qpow(6,mod-2);
void Prime()
{
    for(int i=2;i<=maxn;i++)
    {
        if(!isprime[i])
            prime[prime_cnt++]=i;
        for(int j=i+i;j<=maxn;j+=i)
            isprime[j]=1;
    }
}
int main()
{
    ll t;
    Prime();
    scanf("%lld",&t);
    while(t--)
    {

        scanf("%lld",&k);
        n=k;
        ll sum=n*(n+1)%mod*(2LL*n+1)%mod*six%mod;
        p_cnt=0;

        for(int i=0;i<prime_cnt;i++)
        {
            if(prime[i]>k)break;

            if(k%prime[i]==0)
            {
                p[p_cnt++]=prime[i];
                while(k%prime[i]==0)k/=prime[i];
            }

        }
        if(k>1)p[p_cnt++]=k;

        ll ans=0;
        for(int i=1;i<(1<<p_cnt);i++)
        {
            ll x=1;
            ll cnt=0;
            for(int j=0;j<p_cnt;j++)
            {
                if((i>>j)&1)
                {
                    x=x*p[j]%mod;
                    cnt++;
                }
            }
            k=n/x;
            if(cnt%2)ans=(ans+x*x%mod*k%mod*(k+1)%mod*(2LL*k+1)%mod*six%mod)%mod;
            else ans=((ans-x*x%mod*k%mod*(k+1)%mod*(2LL*k+1)%mod*six%mod)%mod+mod)%mod;

        }
        printf("%lld\n",((sum-ans)%mod+mod)%mod);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41021816/article/details/81939612