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


相关推荐

  • Nginx灰度发布—-【cookie】、【IP】

    Nginx灰度发布—-【cookie】、【IP】

    2021年6月4日
    97
  • springboot 学习笔记(四) 初识actuator

    springboot 学习笔记(四) 初识actuator

    2021年5月28日
    126
  • 基于SSM的景区旅游管理系统

    基于SSM的景区旅游管理系统项目运行截图首页展示信息攻略管理酒店管理技术描述开发工具:idea/eclipse数据库:mysqlJar包仓库:Maven前段框架:Html/Thymeleaf后端框架:spring+springmvc+mybatis+SpringBoot资料说明基于SSM的景区旅游管理系统,整体包含前后台页面,整体采用SSM框架,SpringBoot作为基础框架,前端页面采用Thymele

    2022年5月8日
    59
  • 安卓手机当电脑摄像头(DroidCam网盘及使用方法)

    安卓手机当电脑摄像头(DroidCam网盘及使用方法)网盘密码:7fp3最近校招笔试,没有摄像头,所以打起了手机摄像头的主意,搜了一下,网上的安装包很旧,有的用不了,使用讲的也不全,所以我从官网和Google商店重新下了一遍。安装环境:笔记本win10专业版手机小米mix2s,安卓8.0。从网盘下载文件,分别安装在电脑和手机上。打开手机端和电脑端,把手机界面DroidcamPort后的数字填在电脑上。如…

    2022年6月24日
    48
  • JAVA Map转List

    JAVA Map转List将Map的key转成List:ListmapKeyList=newArrayList(map.keySet());将Map的Value转成List:ListmapValueList=newArrayList(map.valueSet());

    2022年7月26日
    18
  • 计算机教育中缺失的一课,劝学弟学妹们一句,一定要趁早补上,工作后会事半功倍!「建议收藏」

    计算机教育中缺失的一课,劝学弟学妹们一句,一定要趁早补上,工作后会事半功倍!「建议收藏」各位学弟学妹们好,作为稍微年长的我(岁月是把杀猪刀啊),今天就给大家补补课。在大学里的,我们上的计算机专业课程一般都是像操作系统、编译原理、计算机组成原理、计算机网络这些理论课程,还有一些像C语言、Java、.Net这些可以实践的课程,甚至还有可能让你焊一个收音机,但是对于一些基本习惯却很容易被忽略,需要学弟学妹们自行摸索。实际上,一些好的基本习惯是时时刻刻在影响着我们自己的,不仅是在学校的学习生活中,还是在毕业后的工作生活中。今天我要给大家说就是,使用键盘的习惯。有的同学可能会诧异,键盘谁不会用啊,

    2022年7月16日
    22

发表回复

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

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