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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • async/await Task Timeout

    async/await Task Timeout

    2021年9月15日
    63
  • tomcat突然宕机问题解决方案

    tomcat突然宕机问题解决方案一.tomcat突然宕机时间2019年10月19号8点30分51秒,xxxx系统生产环境的92机器出现tomcat突然宕机问题。二.问题定位1.排查tomcat的启停日志。在文件tomcat/logs/localhost.xxxx.log,排查tomcat的启停日志正常。在宕机时刻,有关闭日志输出。2.使用history命令,查看系统的操作命令。发现使用‘./bi…

    2022年7月26日
    32
  • 设置IP地址、子网掩码、网关和DNS(手动获得IP地址默认网关)

    IP地址,子网掩码、默认网关,DNS的设置和工作原理(总结)转载:https://blog.csdn.net/kingshown_WZ/article/details/46423771概念:1.概述IP地址:人们在Internet上为了区分数以亿计的主机而给每台主机分配的一个专门的地址,通过IP地址就可以访问到每台主机。…

    2022年4月15日
    76
  • awvs原理_csgo奇葩武器代码

    awvs原理_csgo奇葩武器代码awvs10.5下载http://www.32r.com/soft/12986.html

    2022年9月22日
    5
  • 卓越性能 の 军火库「建议收藏」

    卓越性能 の 军火库「建议收藏」在介绍性能优化的军火库之前,先来扯几句题外话。希望这些题外话,能打消你追求卓越性能的理想,来甘心当一枚圆滑的钉子。我是非常不推荐程序员,对公司的业务,进行性能优化的。说这话,纯粹是基于个人自身安全考虑。因为性能优化,在大多数公司,属于费力不讨好的工作项。追求极简的代码,性能卓越的代码,是有追求的程序员的目标。但随着经历了大大小小的公司,我发现很多优秀的程序员,在经受着这种追求的反嗜,以至于痛不欲生。下有下面几点原因,虽然我们知道它肯定是错的,但我们无能无力:公司按照完成的功能,对程序员…

    2022年9月30日
    2
  • goland刷新时间永久激活破解方法

    goland刷新时间永久激活破解方法,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月14日
    62

发表回复

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

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