hdu4407 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.
容斥原理
#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 容斥原理的更多相关文章
- HDU 4407 Sum 容斥原理
Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Desc ...
- HDU 1796 Howmany integers can you find (容斥原理)
How many integers can you find Time Limit: 12000/5000 MS (Java/Others) Memory Limit: 65536/32768 ...
- 容斥原理+补集转化+MinMax容斥
容斥原理的思想大家都应该挺熟悉的,然后补集转化其实就是容斥原理的一种应用. 一篇讲容斥的博文https://www.cnblogs.com/gzy-cjoier/p/9686787.html 当我们遇 ...
- HDU-5072 补集转化+容斥原理
题意:给n个数,求满足一下条件的三元组(a,b,c)数量:a,b,c两两互质或者a,b,c两两不互质. 解法:这道题非常巧妙地运用补集转化和容斥原理.首先我们令这n个数为n个点,然后两两之间连边如果是 ...
- Sum(hdu4407)
Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- 牛客网多校训练第一场 F - Sum of Maximum(容斥原理 + 拉格朗日插值法)
链接: https://www.nowcoder.com/acm/contest/139/F 题意: 分析: 转载自:http://tokitsukaze.live/2018/07/19/2018ni ...
- Nowcoder Sum of Maximum ( 容斥原理 && 拉格朗日插值法 )
题目链接 题意 : 分析 : 分析就直接参考这个链接吧 ==> Click here 大体的思路就是 求和顺序不影响结果.故转化一下思路枚举每个最大值对答案的贡献最后累加就是结果 期间计数的过程 ...
- hdu4059 The Boss on Mars(差分+容斥原理)
题意: 求小于n (1 ≤ n ≤ 10^8)的数中,与n互质的数的四次方和. 知识点: 差分: 一阶差分: 设 则 为一阶差分. 二阶差分: n阶差分: 且可推出 性质: 1. ...
- BZOJ 2440: [中山市选2011]完全平方数 [容斥原理 莫比乌斯函数]
2440: [中山市选2011]完全平方数 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3028 Solved: 1460[Submit][Sta ...
随机推荐
- day4-python基础-运算符
本章节主要说明Python的运算符.举个简单的例子 4 +5 = 9 . 例子中,4 和 5 被称为操作数,"+" 称为运算符. Python语言支持以下类型的运算符: 算术运算符 ...
- OOP⑵
1.问题? 怎么创建对象? 类名 对象名=new 类名(); 在java中只要是看到了()! 这就是方法! 2.构造方法: 创建某个对象的方法! Student stu=new Student(); ...
- Saiku的下载与安装(一)
Saiku- 数据可视化的工具,连接数据源展示数据,并且可方便导出xls/csv/pdf等文件的工具 一.Saiku下载 社区网址:https://community.meteorite.bi/ 二. ...
- MATLAB 图片折腾4
重新安排矩阵的x,y,z , 在二维中就相当于把x,y 对换,在三维中相当于可以把三个坐标的位置互换. 比如A = A(:,:,1)=repmat(1,3,3);A(:,:,2)=repmat(2,3 ...
- 图的邻接矩阵存储实现,C++描述
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- 小程序之setData特殊情况 三种情况的wx:if
比如data{ “a”:{}, "b":{} } 你想完成这样的结构 //创建一个对象 var readyData={} //对象[key] =另一个对象 readyData[ke ...
- CentOS7下cratedb备份及恢复(快照)
一:创建存储库 1.1 概要 CREATE REPOSITORY repository_name TYPE type [ WITH (repository_parameter [= value], [ ...
- Docker常用命令学习
sudo service docker startsudo docker run hello-worlddocker stats --helpdocker run -d -P training/web ...
- 20165326 java第二周学习笔记
学习笔记 一.理论学习 基本数据类型与数组 标识符的第一个字符不能是数字:标识符不能为关键字. 基本数据类型多数与c语言相同.重点如下: 1.逻辑类型boolean赋值true/false 2.浮点数 ...
- IDEA如何自动生成testNG的测试报告?
问:eclipse会在test-output目录下自动生成测试报告,想知道IDEA是只可以在控制台那里点击导出手动生成报告么? 答:编译器选择Edit Configuration,找到测试项,找到Li ...