HDU-4407-Sum(容斥原理)「建议收藏」

HDU-4407-Sum(容斥原理)

大家好,又见面了,我是全栈君。

Problem Description
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
   
   
1 3 3 2 2 3 1 1 3 4 1 2 3 6

 


Sample Output
   
   
7 0

 


Source

#include <stdio.h>

int num,idx[1005],val[1005],prime[10],p[40000];

inline bool check(int x)
{
    int i;

    for(i=0;i<num;i++) if(x%prime[i]==0) return 0;

    return 1;
}

int main()
{
    int T,n,m,i,j,k,type,cnt,a,b,c,last,lxdcnt,lxdnum,l,r;
    long long ans;

    //把40W以内的素数预处理出来-----------------
    cnt=0;

    for(i=2;i<400000;i++)
    {
        for(j=2;j*j<=i;j++) if(i%j==0) break;

        if(j*j>i) p[cnt++]=i;
    }
    //------------------------------------------

    scanf("%d",&T);

    while(T--)
    {
        scanf("%d%d",&n,&m);

        cnt=0;

        for(i=1;i<=m;i++)
        {
            scanf("%d",&type);

            if(type==1)
            {
                scanf("%d%d%d",&a,&b,&c);

                ans=(long long)(a+b)*(b-a+1)/2;

                num=0;//质因数的个数

                //获取c的质因数-----------------
                last=0;

                while(c>1)
                {
                    if(c%p[last]==0)
                    {
                        prime[num++]=p[last];
                        c/=p[last];
                        while(c%p[last]==0) c/=p[last];
                    }

                    last++;
                }
                //-------------------------------

                //容斥原理-------------------------
                for(j=1;j<(1<<num);j++)
                {
                    lxdcnt=0;
                    lxdnum=1;

                    for(k=0;k<num;k++) if(j&(1<<k))
                    {
                        lxdcnt++;
                        lxdnum*=prime[k];
                    }


                    l=a/lxdnum*lxdnum;
                    if(l<a) l+=lxdnum;
                    r=b/lxdnum*lxdnum;
                    if(r<l) continue;

                    if(lxdcnt&1) ans-=(long long)(l+r)*((r-l)/lxdnum+1)/2;
                    else ans+=(long long)(l+r)*((r-l)/lxdnum+1)/2;
                }
                //-----------------------------------

                //对改动过的数字特殊推断-----------------------------------
                for(j=0;j<cnt;j++)
                {
                    if(idx[j]>=a && idx[j]<=b)
                    {
                        if(!check(idx[j]))
                        {
                            if(check(val[j])) ans+=val[j];
                        }
                        else
                        {
                            if(check(val[j])) ans=ans+val[j]-idx[j];
                            else ans-=idx[j];
                        }
                    }
                }
                //--------------------------------------------------------

                printf("%I64d\n",ans);
            }
            else
            {
                scanf("%d%d",&a,&b);

                for(j=0;j<cnt;j++) if(idx[j]==a)//注意,改动的点可能之前已被改动过
                {
                    val[j]=b;
                    break;
                }

                if(j==cnt)
                {
                    idx[cnt]=a;
                    val[cnt++]=b;
                }
            }
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115923.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • eclipse怎么导入一个Java项目(莫要错过,最详细教程!)

    eclipse怎么导入一个Java项目(莫要错过,最详细教程!)对于eclipse软件,常规的打开文件方法是无法打开一个项目的,那么怎样导入一个java项目呢?方法如下第一步在电脑打开eclipse软件,点击file->Import,如下图所示:第二步选择General->ExistingProjectsintoworkspace,点击next,如下图所示:第三步点击选择要导入的项目路径,选好,点击finish,如下图所示:到此为止,已经导入成功了如果对你产生了帮助,那么请给博主一个小小的赞哦。…

    2022年7月7日
    30
  • gitee pycharm_怎么把git上放到pycharm

    gitee pycharm_怎么把git上放到pycharm好久没有使用git,都忘记git的操作流程了,只能强制回忆一下:今天接到领导通知,要把我这边写的代码提交到远程仓库,然后就想,他那边仓库里的代码源码都是我提供的,我本地在pycharm中也是添加了git管理的,所以就想直接我这边的仓库代码直接push到远程仓库上去,先pull,然后在push就行了,结果搞了半天,一直提示我这边的版本在远程仓库之前的版本:hint:Updateswererejectedbecausethetipofyourcurrentbranchisbehi

    2022年8月28日
    2
  • python运行不了怎么办_except python

    python运行不了怎么办_except pythonpython将自己写的模块上传到PyPI服务器,报错error:<urlopenerror[SSL:CERTIFICATE_VERIFY_FAILED]certificatever

    2022年7月28日
    3
  • 笔记本windows10连接wifi显示无internet_win10连接wifi显示无internet

    笔记本windows10连接wifi显示无internet_win10连接wifi显示无internetwin10笔记本连上WiFi,显示“无internet,安全”,本文提供了6种解决办法

    2022年8月1日
    2
  • MongoDB和Mysql区别

    MongoDB和Mysql区别mysql和MongoDB的区别?对应优点?mysql是关系型数据库,MongoDB是非关系型数据库。Mysql的优点:处理复杂事务,批sql(交易系统、订单系统、银行系统)。 MongoDB优点:更高的写入负载:侧重数据写入性能,而非事务安全; 储存对象数据方便,类可以直接序列化成JSON储存到MongoDB中。…

    2022年6月18日
    15
  • wind river hypervisor 2.0.2.1

    wind river hypervisor 2.0.2.1

    2022年1月2日
    46

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号