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 ...
随机推荐
- node(2) EventEmitter类 事件队列 事件和error事件方法
事件队列的核心:事件触发与事件监听器功能的封装. // 引入 events 模块 var events = require('events'); // 创建 eventEmitter 对象 var e ...
- 团队项目开发特点以及NABCD分析总结
(注:此博客来源于韩晓凡,我们是一个团队) 团队项目的特点:开发的这款软件是从我们的日常生活中得到的启发,现在正是大学阶段,刚刚开始管理自己每个月的生活费,并且在大学中每个月的生活费会有很多去处,然而 ...
- SQL-14 从titles表获取按照title进行分组,每组个数大于等于2,给出title以及对应的数目t。 注意对于重复的emp_no进行忽略。
题目描述 从titles表获取按照title进行分组,每组个数大于等于2,给出title以及对应的数目t.注意对于重复的emp_no进行忽略.CREATE TABLE IF NOT EXISTS &q ...
- 10.Python-第三方库requests详解(二)
Requests 是用Python语言编写,基于 urllib,采用 Apache2 Licensed 开源协议的 HTTP 库.它比 urllib 更加方便,可以节约我们大量的工作,完全满足 HTT ...
- L293 给地球降温
Countries look at ways to tinker with Earth’s thermostat The idea of cooling the climate with strato ...
- java.lang.OutOfMemoryError: GC overhead limit exceeded
前端请求:{"code":400,"message":"Handler dispatch failed; nested exception is ja ...
- idea创建maven spring项目,出现的问题
1.jetty启动后,明明配置文件配置的是 <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId ...
- Java 继承extends、关键字super和this、多态、动态绑定
Manager 经理 extends Employee 雇员 "is-a"(是)关系是继承的一个明显特征.每个经理都是一名雇员. 从术语的角度讲,Manager是子类,Emplo ...
- 微信小程序中时间转化为时间戳(安卓和苹果兼容性)
在IOS中时间显示NAN,后来才知道是由于安卓和IOS时间解析时间的时间格式不一致: 在安卓机中2018-06-21 16:00:21 IOS识别的格式是 2018/06/21: 所以在处理IOS机器 ...
- 【转载】 【caffe转向pytorch】caffe的BN层+scale层=pytorch的BN层
原文地址: https://blog.csdn.net/u011668104/article/details/81532592 ------------------------------------ ...