Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Problem Description
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.
 
Input
There are several test cases. The first line in the input is an integer indicating the number of test cases. For each case, the first line begins with two integers --- the above mentioned n and m. Each the following m lines contains an operation. Operation 1 is in this format: "1 x y p". Operation 2 is in this format: "2 x c".
 
Output
For each operation 1, output a single integer in one line representing the result.
 
Sample Input
1
3 3
2 2 3
1 1 3 4
1 2 3 6
 
Sample Output
7
0
 
Source
 
#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 容斥原理的更多相关文章

  1. hdu 4407 Sum

    http://acm.hdu.edu.cn/showproblem.php?pid=4407 题意:给定初始n个数1..n,两个操作,①1 x y p  询问第x个数到第y个数中与p互质的数的和; ② ...

  2. HDU - 4407 Sum (容斥)

    题意:初始序列[1..N](1<=N<=4e5),支持两种操作:1.求区间[x,y]内与p互素的数之和: 2.将x位置的数变为c. 分析:很容易把人骗到线段树的思维中,而实际上操作2单点的 ...

  3. hdu 4407 Sum 容斥+当前离线

    乞讨X-Y之间p素数,,典型的纳入和排除问题,列的求和运算总和的数,注意,第一项是最后一个项目数. 如果不改变到第一记录的答案,脱机处理,能保存查询,候,遇到一个操作1,就遍历前面的操作.把改动加上去 ...

  4. HDOJ(HDU).1258 Sum It Up (DFS)

    HDOJ(HDU).1258 Sum It Up (DFS) [从零开始DFS(6)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双 ...

  5. 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 ...

  6. HDU 4407

    http://acm.hdu.edu.cn/showproblem.php?pid=4407 把修改和询问分成两部分解决 询问求区间内与p不互素的和,和求个数一样,用容斥原理解决,只不过做容斥的时候把 ...

  7. hdu 1258 Sum It Up(dfs+去重)

    题目大意: 给你一个总和(total)和一列(list)整数,共n个整数,要求用这些整数相加,使相加的结果等于total,找出所有不相同的拼凑方法. 例如,total = 4,n = 6,list = ...

  8. HDU 4135 Co-prime(容斥原理)

    Co-prime 第一发容斥,感觉挺有意思的 →_→ [题目链接]Co-prime [题目类型]容斥 &题意: 求(a,b)区间内,与n互质的数的个数. \(a,b\leq 10^{15}\) ...

  9. 数论 --- 费马小定理 + 快速幂 HDU 4704 Sum

    Sum Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=4704 Mean: 给定一个大整数N,求1到N中每个数的因式分解个数的 ...

随机推荐

  1. 使用Servlet Filter做Login checking

    1) 建一个Login Servlet: Login.java package com.my; import java.io.*; import javax.servlet.*; import jav ...

  2. 【转】七个例子帮你更好地理解 CPU 缓存

    我的大多数读者都知道缓存是一种快速.小型.存储最近已访问的内存的地方.这个描述相当准确,但是深入处理器缓存如何工作的"枯燥"细节,会对尝试理解程序性能有很大帮助. 在这篇博文中,我 ...

  3. Spring小结

    一.环境搭建 创建Maven项目 一般pom.xml会出错,本地若无相应版本的jar包,则无法下载或下载速度非常慢,我的解决方案是,查找本地仓库的jar,修改为本地仓库有的jar即可 pom.xml的 ...

  4. elixir学习

    安装 brew install elixir atom配置 language-elixir atom-elixir elixir的shell iex :erlang.system_info(:otp_ ...

  5. 关于mac mini组装普液晶显示器

    申请了好久的mac mini,部门终于给买下来了.没想到,买回来之后的组装还是折腾了我们一把.  因为先前没用过mac mini,以为它和普通的台式机一样,买回来就能直接到显示器上用了.结果买回来ma ...

  6. Maven打包web工程成WAR

    其实不一定要通过Goals:package来打war包,直接run as maven bulid也行:

  7. PL/SQL查询Oracle数据乱码/Oracle客户端乱码解决办法

    [如果此方法都试了就是不行,那么就重复尝试,先把环境变量给删了,注册表里的键值也删除了,然后重启,再配置,肯定行!我试过!] 先确定Oracle服务器采用的是何种编码: select userenv( ...

  8. /proc/sys/net/ipv4/

    /proc/sys/net/ipv4/icmp_timeexceed_rate这个在traceroute时导致著名的"Solaris middle star".这个文件控制发送IC ...

  9. shell中大小写转换

    有两种方式: 1.用tr 例如:UPPERCASE=$(echo $VARIABLE | tr '[a-z]' '[A-Z]')   (把VARIABLE的小写转换成大写) LOWERCASE=$(e ...

  10. [JS]setInterval,setTimeout的使用以及伪随机数