HDU - 4407 Sum(分解质因数&等差数列求和&容斥原理)

XXX is puzzled with the question below:

1, 2, 3, …, n (1<=n<=400000) are placed in a line. There are m (1<=m<=1000) operations of two kinds.

Operation 1: among the x-th number to the y-th number (inclusive), get the sum of the numbers which are co-prime with p( 1 <=p <= 400000). 
Operation 2: change the x-th number to c( 1 <=c <= 400000).

For each operation, XXX will spend a lot of time to treat it. So he wants to ask you to help him. 
Input 
There are several test cases. 
The first line in the input is an integer indicating the number of test cases. 
For each case, the first line begins with two integers — the above mentioned n and m. 
Each the following m lines contains an operation. 
Operation 1 is in this format: “1 x y p”. 
Operation 2 is in this format: “2 x c”. 
Output 
For each operation 1, output a single integer in one line representing the result. 
Sample Input 

3 3 
2 2 3 
1 1 3 4 
1 2 3 6 
Sample Output 

0

两种操作:

1.求[x,y]之间与p互素的数的和

2.改变第x位置上的数为c

先采取这样的策略:

不先考虑改变的问题,先把其当成等差数列。先求出[1,y]内与p不互质的数和,减去[1,x-1]间与p不互质的数和,这样得到[x,y]之间与p不互质的数和,再用[x,y]所有数的和减去上述和,就得到所求。

采用的算法有分解质因子:

void prim(LL num)
{
    cnt=0;
    for(LL i=2;i*i<=num;i++)
    {
        if(num%i==0)
        {
            a[cnt++]=i;
            while(num%i==0)num/=i;
        }
    }
    if(num!=1)a[cnt++]=num;
}

例如容斥原理的二进制形式求出不互质数之和:

LL all(LL m)
{
    LL ans=0;///非互质数之和
    for(LL i=1; i<(1<<cnt); i++)
    {
        LL sum=1,num1=0,tmp=i;
        for(LL j=0; j<cnt; j++)
        {
            if(i&((LL)(1<<j)))//表示用哪一个质因子
            {
                sum*=a[j];
                //cout<<i<<" "<<sum<<endl;
                num1++;
            }
        }
        tmp=m/sum;
        if(num1&1) ans+=tmp*(tmp+1)*sum/2;
        else ans-=tmp*(tmp+1)*sum/2;
/*
以12为例,2,3,4,6,8,9,10,12是与之非互质的数,而2,3是其质因子;当选2的时候,筛去2,4,6,8,10,12
也就是2(1+2+3+4+5+6);tmp=12/2=6个非互质数;求和:2*(tmp+1)*(tmp)/2;加上
当选3的时候重复;加上
当选2,3的时候,减去其公倍数6,12即得到最终答案;
*/
    }
    return ans;
}

接着考虑变化后的值,即第k位数可能变化,若原来的它是与之互质,减去;再考虑后来变化的值,若与之互质加上

代码:

prim(p);
                ans=x*(y-x+1)+(y-x+1)*(y-x)/2;
//                printf("----%lld    %lld  %lld\n",ans,all(y),all(x-1));
                ans-=all(y)-all(x-1);
                for(it=vis.begin(); it!=vis.end(); it++)
                {
                    if(it->first>=x&&it->first<=y)
                    {
                        if(gcd(it->first,p)==1)ans-=it->first;
                        if(gcd(it->second,p)==1)ans+=it->second;
                    }
                }

总体代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
#define LL long long
using namespace std;
LL t,n,m,p,a[100080];
LL cnt=0;
map<LL,LL>vis;
map<LL,LL>::iterator it;
void prim(LL num)
{
    cnt=0;
    for(LL i=2;i*i<=num;i++)
    {
        if(num%i==0)
        {
            a[cnt++]=i;
            while(num%i==0)num/=i;
        }
    }
    if(num!=1)a[cnt++]=num;
}
LL gcd(LL a,LL b)
{
    return b==0?a:gcd(b,a%b);
}
LL all(LL m)
{
    LL ans=0;///非互质数之和
    for(LL i=1; i<(1<<cnt); i++)
    {
        LL sum=1,num1=0,tmp=i;
        for(LL j=0; j<cnt; j++)
        {
            if(1&tmp)
            {
                sum*=a[j];
                num1++;
            }
            tmp>>=1;
        }
        tmp=m/sum;
        if(num1&1) ans+=tmp*(tmp+1)*sum/2;
        else ans-=tmp*(tmp+1)*sum/2;
    }
    return ans;
}
int main()
{
    scanf("%lld",&t);
    while(t--)
    {
        scanf("%lld%lld",&n,&m);
        vis.clear();
        while(m--)
        {
            LL com,x,y;
            LL ans=0;
            scanf("%lld",&com);
            if(com==1)
            {
                scanf("%lld%lld%lld",&x,&y,&p);
                prim(p);
                ans=x*(y-x+1)+(y-x+1)*(y-x)/2;
//                printf("----%lld    %lld  %lld\n",ans,all(y),all(x-1));
                ans-=all(y)-all(x-1);
                for(it=vis.begin(); it!=vis.end(); it++)
                {
                    if(it->first>=x&&it->first<=y)
                    {
                        if(gcd(it->first,p)==1)ans-=it->first;
                        if(gcd(it->second,p)==1)ans+=it->second;
                    }
                }
                printf("%lld\n",ans);
            }
            else
            {
                scanf("%lld%lld",&x,&p);
                vis[x]=p;
            }
        }
    }
}

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值