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.

容斥原理

 #include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
typedef long long ll; const int maxn=4e5+; inline int gcd(int a,int b){return b?gcd(b,a%b):a;} int ori[],cha[];
int pnum[],num; int main(){
int T;
scanf("%d",&T);
while(T--){
int n,m;
scanf("%d%d",&n,&m);
int cnt=;
while(m--){
int f;
scanf("%d",&f);
if(f==){
int x,y,p;
scanf("%d%d%d",&x,&y,&p);
int tmp=p;
num=;
for(int i=;i*i<=tmp;++i){
if(!(tmp%i)){
pnum[++num]=i;
tmp/=i;
while(!(tmp%i))tmp/=i;
}
}
if(tmp>)pnum[++num]=tmp;
ll ans=;
for(int i=;i<(<<num);++i){
int bit=;
ll mul=;
for(int j=;j<=num;++j){
if(i&(<<(j-))){
bit++;
mul*=pnum[j];
}
}
ll tmp=y/mul-(x-)/mul;
ll l=((x-)/mul+)*mul,r=y/mul*mul;
tmp=(l+r)*tmp/;
if(bit%)ans+=tmp;
else ans-=tmp;
}
ans=(x+y)*(ll)(y-x+)/-ans;
for(int i=;i<=cnt;++i){
if(ori[i]>=x&&ori[i]<=y){
int gcd1=gcd(ori[i],p),gcd2=gcd(cha[i],p);
if(gcd1==&&gcd2>)ans-=ori[i];
else if(gcd1>&&gcd2==)ans+=cha[i];
else if(gcd1==&&gcd2==)ans=ans-ori[i]+cha[i];
}
}
printf("%lld\n",ans);
}
else{
int x,c;
scanf("%d%d",&x,&c);
bool f=;
for(int i=;i<=cnt;++i){
if(ori[i]==x){
cha[i]=c;
f=;
break;
}
}
if(f){
++cnt;
ori[cnt]=x;
cha[cnt]=c;
}
}
}
}
return ;
}

hdu4407 Sum 容斥原理的更多相关文章

  1. HDU 4407 Sum 容斥原理

    Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Desc ...

  2. HDU 1796 Howmany integers can you find (容斥原理)

    How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  3. 容斥原理+补集转化+MinMax容斥

    容斥原理的思想大家都应该挺熟悉的,然后补集转化其实就是容斥原理的一种应用. 一篇讲容斥的博文https://www.cnblogs.com/gzy-cjoier/p/9686787.html 当我们遇 ...

  4. HDU-5072 补集转化+容斥原理

    题意:给n个数,求满足一下条件的三元组(a,b,c)数量:a,b,c两两互质或者a,b,c两两不互质. 解法:这道题非常巧妙地运用补集转化和容斥原理.首先我们令这n个数为n个点,然后两两之间连边如果是 ...

  5. Sum(hdu4407)

    Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  6. 牛客网多校训练第一场 F - Sum of Maximum(容斥原理 + 拉格朗日插值法)

    链接: https://www.nowcoder.com/acm/contest/139/F 题意: 分析: 转载自:http://tokitsukaze.live/2018/07/19/2018ni ...

  7. Nowcoder Sum of Maximum ( 容斥原理 && 拉格朗日插值法 )

    题目链接 题意 : 分析 : 分析就直接参考这个链接吧 ==> Click here 大体的思路就是 求和顺序不影响结果.故转化一下思路枚举每个最大值对答案的贡献最后累加就是结果 期间计数的过程 ...

  8. hdu4059 The Boss on Mars(差分+容斥原理)

    题意: 求小于n (1 ≤ n ≤ 10^8)的数中,与n互质的数的四次方和. 知识点: 差分: 一阶差分: 设  则    为一阶差分. 二阶差分: n阶差分:     且可推出    性质: 1. ...

  9. BZOJ 2440: [中山市选2011]完全平方数 [容斥原理 莫比乌斯函数]

    2440: [中山市选2011]完全平方数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3028  Solved: 1460[Submit][Sta ...

随机推荐

  1. unity实现用鼠标右键控制摄像机视角上下左右移动

    using System;using System.Collections.Generic;using UnityEngine;public class ViewControl{ enum Rotat ...

  2. loj6277

    题解: 树状数组模板提 代码: #include<bits/stdc++.h> using namespace std; ; int num[N],n,a[N],l,r,c,opt; vo ...

  3. SignalR 前期简单配置

    一.随便你在哪个命名空间下新建一个Startup类,并在在该类中注册SignalR. using Owin; using System; using System.Collections.Generi ...

  4. hdu 1057 A + B Again

    A + B Again Problem Description There must be many A + B problems in our HDOJ , now a new one is com ...

  5. Linux3.10.0块IO子系统流程(2)-- 构造、排序、合并请求

    Linux块设备可以分为三类.分别针对顺序访问物理设备.随机访问物理设备和逻辑设备(即“栈式设备”)   类型 make_request_fn request_fn 备注 SCSI 设备等 从bio构 ...

  6. transiton,transform,animation,border-image

    animation,transition,transform三者联系与区别: https://www.jianshu.com/p/0e0e1903b80d transform: 使用小技巧: tran ...

  7. SharePoint online Multilingual support - Development(2)

    博客地址:http://blog.csdn.net/FoxDave 上一节讲了如何通过Code的方式设置Site和List级别的国际化,本节介绍一下如何设置Content type和Site co ...

  8. android小程序-电子钢琴-滑动连续响应

    原创文字,转载请标明出处: 利用Button实现简单地电子钢琴,可以简单地响应按钮的click事件来发出相应的声音.但是这样不能达到手指在屏幕滑动,而连续发声的效果,就像手指在真实钢琴按键上滑过一样. ...

  9. Windows10下pip的配置文件设置

    pip.ini的内容: [global] index-url = http://mirrors.aliyun.com/pypi/simple trusted-host = mirrors.aliyun ...

  10. Eclipse_设置_01_自动提示