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中每个数的因式分解个数的 ...
随机推荐
- Splashscreen
Splashscreen Enables developers to show/hide the application's splash screen. Methods show hide Perm ...
- C# MDI 子窗体被父窗体控件挡住
using System.Runtime.InteropServices; [DllImport("user32")] public static extern int SetPa ...
- hyper-v 中 安装 Centos 7.0 设置网络 教程
安装环境是: 系统:win server 2012 r2 DataCenter hyper-v版本:6.3.9600.16384 centos版本:7.0 从网上下载的 centos 7.0 如果找 ...
- mongodb基本语句使用
mongodb学习:##mongodb基础##数据库常用命令##用户相关##修改.添加.删除集合数据##条件操作符##创建表 ------------------------------------- ...
- 【Hadoop需要的Jar包】Hadoop编程-pom.xml文件
JDK版本的要求 Hadoop 2.7 以及之后的版本,需要JDK 7: Hadoop 2.6 以及之前的版本,支持JDK 6: 对于Hadoop1.x.x版本,只需要引入1个jar: hadoop- ...
- Windows原生MPIO存储多路径软件详解与应用
介绍 在Windows Server 2008和Windows Server 2008 R2中开始支持Native Multipathing(MPIO)软件作为操作系统的一个组件存在.EMC旗下的存储 ...
- MongoDB:Replica Set 之操作日志 Oplog
转载地址:http://francs3.blog.163.com/blog/static/4057672720121133328120/ 之前的blog 学习了 MongoDB 主从搭建,以及节点管 ...
- 删除SQL server 实例
在网上找到下面几种方法,本人使用的是第一种,很实用. 1.删除 SQL Server 的特定实例若要删除 SQL Server 的某个特定实例,请按照以下步骤操作: 找到并删除%drive%:\\Pr ...
- ie浏览器的渲染原理
IE下载或者渲染顺序大致如下: IE下载的顺序是从上到下,渲染的顺序也是从上到下,下载和渲染是同时进行的. 在渲染到页面的某一部分时,其上面的所有部分都已经下载完成(但并不是说所有相关联的元素都已经下 ...
- Linux命令(18)查看当前用户who、whoami、who am i
首先看命令的使用情况: [@sjs_9_108 ~]$ whoami spider [@sjs_9_108 ~]$ who am i spider pts/ -- : (192.168.1.1) [@ ...