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 ...
随机推荐
- 【oracle】oracle REGEXP_SUBSTR分割字符串
REGEXP_SUBSTR的使用:通过REGEXP_SUBSTR进行数据分割: , LEVEL, 'i') AS STR FROM DUAL CONNECT BY LEVEL <= LENGTH ...
- assert用法
assert宏的原型定义在<assert.h>中,其作用是如果它的条件返回错误,则终止程序执行,原型定义: void assert( int expression ); assert的作用 ...
- shutil 模块
import shutil #用于简化文件操作的模块 # f1 = open(r"D:\上海python全栈4期\day20\7.shutil模块.py","rb&quo ...
- C#中使用FFMPEG切割、合并视频。
参考网址:https://blog.csdn.net/samwang_/article/details/70332924 使用前先确保电脑已经安装了FFMPEG,并且配置好环境变量.检测是否安装配置好 ...
- Java日期时间,以及相互转换
Java日期时间,以及相互转化 package com.study.string; import java.text.ParseException; import java.text.SimpleDa ...
- xml解析与生成的学习资料
xml解析与生成的学习资料:http://blog.csdn.net/u012325167/article/category/6129813 ----------------------------- ...
- QuickHit 项目
package cn.javaoppday01; import java.util.Random; public class Game { public Player player; public G ...
- nginx的相关配置记录和总结
前言 本文旨在对nginx的各项配置文件和参数做一个记录和总结. 原因是在配置框架和虚拟目录,web语言解析的nginx环境的时候遇到各种问题和参数,有时百度可以解决,有时直接复制粘贴,大都当时有些记 ...
- django面试五
http和https的区别https协议需要到ca申请证书,一般免费证书很少,需要交费. 注:CA - certificate authority,身份认证,权威机构认证,CA认证: http是超文本 ...
- python 爬虫时间数据-时间格式转换
1 import time,datetime 2 3 time_original = '17/Sep/2012:11:40:00' 4 time_format = datetime.datetime. ...