http://acm.hdu.edu.cn/showproblem.php?pid=4407

把修改和询问分成两部分解决

询问求区间内与p不互素的和,和求个数一样,用容斥原理解决,只不过做容斥的时候把每一段的个数改成每一段的和,这个求和的方式我一下写搓了,导致这道题坑了很久

修改用map记录,每次扫一遍即可,原数和c互素就减掉,修改完的数和c互素就加上去,logn*m^2的复杂度

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <cmath> using namespace std; typedef __int64 ll; ll gcd(ll a,ll b){
return b==?a:gcd(b,a%b);
} ll sum(ll l,ll r,ll k){
ll num=r/k-(l-)/k;
ll front;
if(l%k==)front=l;
else front=(k-l%k)+l;
ll rear=r-r%k;
return (front+rear)*num/;
} ll gao(ll l,ll r,ll n){
vector <ll> v;
for(ll i=;i*i<=n;i++){
if(n%i==){
v.push_back(i);
while(n%i==)n/=i;
}
}
if(n>)v.push_back(n);
int m=v.size();
ll res=;
for(int i=;i<(<<m);i++){
int cnt=;
ll val=;
for(int j=;j<m;j++){
if(i&(<<j)){
cnt++;
val*=v[j];
}
}
if(cnt&)res+=sum(l,r,val);
else res-=sum(l,r,val);
}
return (l+r)*(r-l+)/-res;
} map <ll,ll> mp;
map <ll,ll>::iterator it; ll gan(ll x,ll y,ll p){
ll res=gao(x,y,p);
for(it=mp.begin();it!=mp.end();it++){
if(it->first>y || it->first<x)continue;
if(gcd(it->first,p)==)res-=it->first;
if(gcd(it->second,p)==)res+=it->second;
}
return res;
} int main(){
int T;
scanf("%d",&T);
while(T--){
int n,m;
scanf("%d%d",&n,&m);
mp.clear();
while(m--){
int op;
scanf("%d",&op);
ll x,y,p;
if(op==){
scanf("%I64d%I64d%I64d",&x,&y,&p);
if(x>y)swap(x,y);
printf("%I64d\n",gan(x,y,p));
}
else{
scanf("%I64d%I64d",&x,&p);
mp[x]=p;
}
}
}
return ;
}

HDU 4407的更多相关文章

  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 容斥原理

    Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Desc ...

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

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

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

  5. HDU - 4407 Sum (容斥)

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

  6. [GodLove]Wine93 Tarining Round #6

    比赛链接: http://vjudge.net/contest/view.action?cid=47642#overview 题目来源: 2012 ACM/ICPC Asia Regional Jin ...

  7. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  8. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  9. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

随机推荐

  1. 关于C#迭代器

    >1 IEnumerator与IEnumerable IEnumerator与IEnumerable两个接口是用于实现迭代器的接品只要实现了IEnumerable就可以用foreach,linq ...

  2. Mysql执行Update操作时会锁住表

    update tableA a,(select a.netbar_id,sum(a.reward_amt) reward_amt from tableB a group by a.netbar_id) ...

  3. CSS 框模型——规定了元素框处理元素内容、内边距、边框和外边距的方式

    转自:http://www.w3school.com.cn/css/css_boxmodel.asp 要知道在父元素:float, rel, abs位置情况下,box模型的变换情况,请见:http:/ ...

  4. Eclipse如何设置代码提示功能

    Windows→Preference→XML→XML Files→Editor→Content Assist→Auto Activation→Prompt when these characters ...

  5. / etc / init.d / iptables: line 268: restorecon: command not found

    When I tried to restart iptables from vps , I got the following error. Iptables encountered such a p ...

  6. LoadRunner 文本检查点使用

    在测试时,我们可以根据文本检查点判断事务是否执行正确.   我们在启动流程时,成功返回:   我们检查这个响应结果是否有. int i=0; i=web_reg_find("Text=\&q ...

  7. C++ STL pair

    没有找到priority_queue里存放pair不用typedef的方法...大概第一次觉得这个有用吧... 优先队列里和sort函数对pair 的默认排序是first从小到大,second从小到大 ...

  8. OpenGL 和OpenGL ES简介

    OpenGL的全称是Open  Graphics  Library,即开放的图形库接口,它定义了一个跨编程语言.跨平台的编程接口的规范,它主要用于三维图形(实际上二维图形也可以)变成.OpenGL的前 ...

  9. 存储过程Oracle(一)

    一.简介 存储过程:就是在数据库中创建的一段程序,供别人调用 .其实我感觉跟定义一个方法相似 二.无参存储过程 如下,经典的输出"Hello World"来入门存储过程 创建一个存 ...

  10. addChildViewController

    http://www.cnblogs.com/zengyou/p/3386605.html //在parent view controller 中添加 child view controller Fi ...