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. jsp下载excel文件

    jsp下载excel文件的的实现方法很多,今天也遇到这个问题,乱敲了一阵,终于搞定了,记下来和朋友们分享吧. 假设需要下载excel文件的jsp页面名为:down.jsp 对应的后台action名为: ...

  2. jdk重装后com.sun.tools.javac.Main is not on the classpath的问题 .

    在重装了JDk之后,在编译工程的时候出现如下错误: com.sun.tools.javac.Main is not on the classpath.Perhaps JAVA_HOME does no ...

  3. loj6277

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

  4. java获得当前系统时间三种方法

    参见: http://blog.csdn.net/cloume/article/details/46624637

  5. nginx在linux上的安装与配置详解(一)

    Nginx的安装与配置详解 (1)nginx简介     nginx概念: Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like ...

  6. Valgrind,内存调试工具

    Valgrind是一款用于内存调试.内存泄漏检测以及性能分析的软件开发工具 官网:http://valgrind.org/ 用户开发手册地址:http://valgrind.org/docs/manu ...

  7. 十七. Python基础(17)--正则表达式

    十七. Python基础(17)--正则表达式 1 ● 正则表达式 定义: Regular expressions are sets of symbols that you can use to cr ...

  8. 深入理解java虚拟机---内存分配策略(十三)

    转载请注明原文地址:https://blog.csdn.net/initphp/article/details/30487407 Java内存分配策略 使用的ParNew+Serial Old收集器组 ...

  9. BootstrapValidator验证

    相关链接: https://www.cnblogs.com/wuwenshuai/p/7120755.html https://www.cnblogs.com/v-weiwang/p/4834672. ...

  10. session会话示例

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...