HDU 4407 Sum 容斥原理
Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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.
3 3
2 2 3
1 1 3 4
1 2 3 6
0
#include <iostream>
#include <stdio.h>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
#include <set>
#include <algorithm>
#include <map>
#include <stack>
#include <math.h>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std ;
typedef long long LL ;
const int M_P= ;
bool isprime[M_P+] ;
int prime[M_P] ,id ;
void make_prime(){
id= ;
memset(isprime,,sizeof(isprime)) ;
for(int i=;i<=M_P;i++){
if(!isprime[i])
prime[++id]=i ;
for(int j=;j<=id&&i*prime[j]<=M_P;i++){
isprime[i*prime[j]]= ;
if(i%prime[j]==)
break ;
}
}
}
int gcd(int x ,int y){
return y==?x:gcd(y,x%y) ;
}
struct Change{
int id ;
int num ;
};
vector<Change>query ;
inline LL Sum(LL N){
return N*(+N)/ ;
}
LL gao(int N ,int P){
vector<int>vec ;
vec.clear() ;
LL ans= ;
LL M=P ;
for(int i=;i<=id&&prime[i]*prime[i]<=M;i++){
if(M%prime[i]==){
vec.push_back(prime[i]) ;
while(M%prime[i]==)
M/=prime[i] ;
}
if(M==)
break ;
}
if(M!=)
vec.push_back(M) ;
int n=vec.size() ;
for(int i=;i<(<<n);i++){
int now= ;
int pri= ;
for(int j=;j<n;j++){
if(i&(<<j)){
now++ ;
pri*=vec[j] ;
}
}
if(now&)
ans=ans+pri*Sum(N/pri) ;
else
ans=ans-pri*Sum(N/pri) ;
}
return Sum(N)-ans ;
}
map<int ,int>my_hash ;
int main(){
make_prime() ;
int L ,R ,P ,N ,M ,T ,kind;
scanf("%d",&T) ;
while(T--){
scanf("%d%d",&N,&M) ;
query.clear() ;
while(M--){
scanf("%d",&kind) ;
if(kind==){
scanf("%d%d%d",&L,&R,&P) ;
if(L>R)
swap(L ,R) ;
LL ans=gao(R,P)-gao(L-,P) ;
my_hash.clear() ;
for(int i=;i<query.size();i++){
int ID=query[i].id ;
int Num=query[i].num ;
if(L<=ID&&ID<=R){
if(my_hash.find(ID)==my_hash.end()){
if(gcd(ID,P)==)
ans-=ID ;
}
else{
LL now_num = my_hash[ID] ;
if(gcd(now_num,P)==)
ans-=now_num ;
}
if(gcd(Num,P)==)
ans+=Num ;
my_hash[ID]=Num ;
}
}
printf("%I64d\n",ans) ;
}
else{
Change now ;
scanf("%d%d",&now.id,&now.num) ;
query.push_back(now) ;
}
}
}
return ;
}
HDU 4407 Sum 容斥原理的更多相关文章
- hdu 4407 Sum
http://acm.hdu.edu.cn/showproblem.php?pid=4407 题意:给定初始n个数1..n,两个操作,①1 x y p 询问第x个数到第y个数中与p互质的数的和; ② ...
- HDU - 4407 Sum (容斥)
题意:初始序列[1..N](1<=N<=4e5),支持两种操作:1.求区间[x,y]内与p互素的数之和: 2.将x位置的数变为c. 分析:很容易把人骗到线段树的思维中,而实际上操作2单点的 ...
- hdu 4407 Sum 容斥+当前离线
乞讨X-Y之间p素数,,典型的纳入和排除问题,列的求和运算总和的数,注意,第一项是最后一个项目数. 如果不改变到第一记录的答案,脱机处理,能保存查询,候,遇到一个操作1,就遍历前面的操作.把改动加上去 ...
- HDOJ(HDU).1258 Sum It Up (DFS)
HDOJ(HDU).1258 Sum It Up (DFS) [从零开始DFS(6)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双 ...
- hdu 4407 容斥原理
题意: 1 //一组数据 3 3 //数字为1-3,3次运算 2 2 3 //将2号位变成3 1 1 3 4 //计算1-3号位上与4互质的数的和 1 2 3 6 好题,需要重复练习 #include ...
- HDU 4407
http://acm.hdu.edu.cn/showproblem.php?pid=4407 把修改和询问分成两部分解决 询问求区间内与p不互素的和,和求个数一样,用容斥原理解决,只不过做容斥的时候把 ...
- hdu 1258 Sum It Up(dfs+去重)
题目大意: 给你一个总和(total)和一列(list)整数,共n个整数,要求用这些整数相加,使相加的结果等于total,找出所有不相同的拼凑方法. 例如,total = 4,n = 6,list = ...
- HDU 4135 Co-prime(容斥原理)
Co-prime 第一发容斥,感觉挺有意思的 →_→ [题目链接]Co-prime [题目类型]容斥 &题意: 求(a,b)区间内,与n互质的数的个数. \(a,b\leq 10^{15}\) ...
- 数论 --- 费马小定理 + 快速幂 HDU 4704 Sum
Sum Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=4704 Mean: 给定一个大整数N,求1到N中每个数的因式分解个数的 ...
随机推荐
- systemd的原理和适用方法
systemd的原理: https://www.linux.com/learn/tutorials/527639-managing-services-on-linux-with-systemd htt ...
- 关于css中列表(ul ol)存在默认间距的问题
一.总结: 有时候我们要给列表(ul ol 本身就是属于块级元素)的上表框或下边框设置颜色,如下: 但是在给内联块级元素(inline-block)的上表框或下边框设置颜色的时候,就没有这么简单: 在 ...
- 【java】之转码
GBK->UTF-8 String str = "任意字符串"; str = new String(str.getBytes("gbk"),"u ...
- 我的IT相关网址收藏
it语言学习免费视频: 尚学堂:http://www.sxt.cn/ 慕课网:http://www.imooc.com/course/list 大学生自学网:http://v.dxsbb.com/ 尚 ...
- 黄聪:jquery mobile通过a标签页面跳转后,样式丢失、js失效的解决方法
问题描述: 用ajax跳转的时候,从a.html跳转到b.html后,b.html的css以及js都失效了. 解决办法1: 将所有的css以及js全部放在div内. 原理: 由于jqm的ajax跳转的 ...
- zookeeper进行leader选举
一.如何进行leader选举 创建 /lj/producer和/lj/master/producer外层节点 创建临时顺序节点 判断自己是否是master节点(判断流程:遍历/lj/producer节 ...
- Navicat(连接) -1高级设置
高级设置 设置位置当创建一个新的连接,Navicat 将在设置位置创建一个子文件夹.大多数文件都保存在该子文件夹: Navicat 对象 服务器类型 扩展名 查询 全部 .sql 导出查询结果设置文件 ...
- VS 开发工具中的Remote Debug 功能远程调试程序经验分享
前言: 有时候我们Dev(开发人员)需要debug tester(测试人员)或者customer(客户)的环境,可tester的机器上没有Code,是不是有点着急? 而且是多版本应用且tester 发 ...
- *.hbm.xml讲解
<!-- package声明pojo类所在的包,如果不写 那么在class中需要指明pojo类所在的包 schema指数据库模式 一个模式下可以有多张表 --> <hibernate ...
- ARM NEON 编程系列2 - 基本指令集
ARM NEON 编程系列2 - 基本指令集 前言 本系列博文用于介绍ARM CPU下NEON指令优化. 博文github地址:github 相关代码github地址:github NEON指令集 主 ...