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中每个数的因式分解个数的 ...
随机推荐
- 运行ASP程序报错
错误提示: An error occurred on the server when processing the URL. Please contact the system administrat ...
- C#时间日期格式大全
C#时间/日期格式大全,C#时间/日期函数大全 有时候我们要对时间进行转换,达到不同的显示效果 默认格式为:2016-7-1 14:33:34 如果要换成成201607,07-2016,2016-7- ...
- php 连接测试sphinx
shpinx.php <?php header("Content-type:text/html;charset=utf-8"); include 'SphinxClient. ...
- Android外派(安卓外派) — 长年提供安卓开发工程师外派业务(可签合同)
北京动点飞扬长年提供安卓工程师外派业务. 平均技术情况如下: 1.2~3年以上Android平台开发经验2.熟练掌握java技术,熟悉面向对象编程设计3.熟悉Android应用开发框架及Activit ...
- PYTHON对文件及文件夹的一些操作
python中对文件.文件夹的操作需要涉及到os模块和shutil模块. 创建文件:1) os.mknod("test.txt") 创建空文件2) open("test. ...
- mongoVUE对mongodb常用的基础操作
一. 连接mongodb服务端: 1. 双击mongoVUE,进入如下图所示界面: 2. 点击上图中的“+”,出现如下图,输入要连接的mongodb服务器的ip. ...
- 3. Map与Tuple
一. Map 对偶 (1)对偶是一个映射.多个对偶形成map (2)对偶的表示:(k,v)或者k->v Map(哈希类型) (1)map的声明与查询 scala> val map1 = M ...
- import了sun开头的类,虽然它在代码里压根就没派上用处!但是必须得引用!
package action; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io. ...
- sql语句(mysql)
MySQL1.数据库数据类型 int 整型 double 浮点型 如double(5,2)最多5位,其中有两位小数 char:固定长度字符串,如char(255),数据长度不足会用空格补足 varch ...
- Instant Run
http://tools.android.com/tech-docs/instant-run N Developer Preview users: Instant Run is currently i ...