题意:

1  //一组数据
3 3    //数字为1-3,3次运算
2 2 3    //将2号位变成3
1 1 3 4    //计算1-3号位上与4互质的数的和
1 2 3 6

好题,需要重复练习

 //
#include<stdio.h>
#include<iostream>
#include<map>
#include<set>
#include<algorithm>
#include<string.h>
#include<stdlib.h>
using namespace std; int gcd(int a,int b)
{
if(b==)return a;
return gcd(b,a%b);
} long long sum1(int x,int y,int p)//求区间[x,y]中p的倍数的和
{
if(p>y)return ;
int t1=x/p;
int t2=y/p;
if(t1*p<x)t1++;
if(t2<t1)return ;
long long sum=;
sum=(long long)p*(t1+t2)*(t2-t1+)/;
return sum;
} const int MAXN=;
int prime[MAXN+];
int getPrime()//得到小于等于MAXN的素数,prime[0]存放的是个数
{
memset(prime,,sizeof(prime));
for(int i=;i<=MAXN;i++)
{
if(!prime[i]) prime[++prime[]]=i;
for(int j=;j<=prime[]&&prime[j]<=MAXN/i;j++)
{
prime[prime[j]*i]=;
if(i%prime[j]==) break;
}
}
return prime[];
}
long long factor[][];
int facCnt;
int getFactors(long long x)//把x进行素数分解
{
facCnt=;
long long tmp=x;
for(int i=;prime[i]<=tmp/prime[i];i++)
{
factor[facCnt][]=;
if(tmp%prime[i]==)
{
factor[facCnt][]=prime[i];
while(tmp%prime[i]==)
{
factor[facCnt][]++;
tmp/=prime[i];
}
facCnt++;
}
}
if(tmp!=)
{
factor[facCnt][]=tmp;
factor[facCnt++][]=;
}
return facCnt;
} long long SS(int s,int x,int y)
//求[x,y]之间是素因子倍数的和。素因子的状态是s,0表示没有这个素因子,
//1表示有。容斥原理。先不管加减,先奇数加,偶数加,最后按照正负去调整就好了。
{
long long ans=;
int cnt=;
int p=;
for(int i=;i<facCnt;i++)
{
if(s&(<<i))
{
cnt++;
p*=factor[i][]; //由于类似2,3,会导致在计算6时重复计算,所以所有素因子都要计算一遍
}
}
ans=sum1(x,y,p);
//printf("%d %d * %d *\n",x,y,p);
if(cnt%)ans=-ans;
//printf("**%lld\n",ans);
return ans;
} long long solve1(int x,int y,int p)//求[x,y]之间和p不互素的数的和
{
getFactors(p);
long long ans=;
for(int i=;i<(<<facCnt);i++)
ans+=SS(i,x,y);
if(ans<)ans=-ans;
return ans;
}
map<int,int>mp;
map<int,int>::iterator it; long long query(int x,int y,int p)//查询。改变被修改了的就可以了
{
long long ans=(long long)(x+y)*(y-x+)/; //原本的和
long long temp=solve1(x,y,p); //与p不互质的数的和
ans-=temp;
for(it=mp.begin();it!=mp.end();it++)
{
int t1=it->first;
if(t1<x||t1>y)continue;
int t2=it->second;
if(gcd(t1,p)==)ans-=t1;//本来互素的要减掉
if(gcd(t2,p)==)ans+=t2;//修改后互素的要加上
}
return ans;
}
int main()
{
getPrime();
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
#endif
int T;
int n,m;
int x,y,p;
int t;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
mp.clear();
while(m--)
{
scanf("%d",&t);
if(t==)
{
scanf("%d%d%d",&x,&y,&p);
if(x>y)swap(x,y);
printf("%I64d\n",query(x,y,p));
}
else
{
scanf("%d%d",&x,&p);
mp[x]=p;
}
}
}
return ;
}

hdu 4407 容斥原理的更多相关文章

  1. GCD HDU - 1695 容斥原理(复杂度低的版本)

    题意: 让你从区间[a,b]里面找一个数x,在区间[c,d]里面找一个数y.题目上已经设定a=b=1了.问你能找到多少对GCD(x,y)=k.x=5,y=7和y=5,x=7是同一对 题解: 弄了半天才 ...

  2. - Visible Trees HDU - 2841 容斥原理

    题意: 给你一个n*m的矩形,在1到m行,和1到n列上都有一棵树,问你站在(0,0)位置能看到多少棵树 题解: 用(x,y)表示某棵树的位置,那么只要x与y互质,那么这棵树就能被看到.不互质的话说明前 ...

  3. HDU 4407 Sum 容斥原理

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

  4. HDU 4407

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

  5. HDU 4059 容斥原理+快速幂+逆元

    E - The Boss on Mars Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  6. hdu 4407 Sum

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

  7. HDU 1796 容斥原理 How many integers can you find

    题目连接   http://acm.hdu.edu.cn/showproblem.php?pid=1796 处男容斥原理  纪念一下  TMD看了好久才明白DFS... 先贴代码后解释 #includ ...

  8. HDU 4336 容斥原理 || 状压DP

    状压DP :F(S)=Sum*F(S)+p(x1)*F(S^(1<<x1))+p(x2)*F(S^(1<<x2))...+1; F(S)表示取状态为S的牌的期望次数,Sum表示 ...

  9. HDU 1796 容斥原理

    How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 ...

随机推荐

  1. Spark记录-Scala数组/List/Map/Set

    import Array._ import scala.collection.mutable.Set object DataStructure { def main(args:Array[String ...

  2. Facebook人工智能实验室的前世今生

    Facebook人工智能实验室的前世今生 是时候停止把Facebook当作纯粹的社交媒体公司来看了.它用无人机提供互联网服务,为了发展虚拟现实而收购Oculus,不懈追求人工智能,Facebook已经 ...

  3. WordPress中使用Markdown和Syntax Highlighter

    下载安装插件 在wordpress插件中安装WP Code Prettify. PHP Markdown Extra 下载Extra,并上传安装到wordpress. Code Prettify th ...

  4. HDU 1728 逃离迷宫 BFS题

    题目描述:输入一个m*n的地图,地图上有两种点,一种是 . 表示这个点是空地,是可以走的,另一种是 * ,表示是墙,是不能走的,然后输入一个起点和一个终点,另外有一个k输入,现在要你确定能否在转k次弯 ...

  5. c++刷题(39/100)笔试题3

    题目1: 现在你需要用一台奇怪的打字机书写一封书信.信的每行只能容纳宽度为100的字符,也就是说如果写下某个字符会导致行宽超过100,那么就要另起一行书写 信的内容由a-z的26个小写字母构成,而每个 ...

  6. 第8月第12天 python json.dumps danmu

    1.json.dumps return JsonResponse({ 'status': WechatMessage.POST_METHOD_REQUIRED[1], 'status_code': W ...

  7. 安装asp.net mvc4后mvc3项目编译报错

    安装asp.net mvc4之后,之前的mvc3项目编译时报这个错“The type System.Web.Mvc.ModelClientValidationRule exists in both c ...

  8. eclipse安装插件的常用方法

    安装插件1.从eclipse安装压缩jar包,如安装svn工具包:eclipse_svn_site-1.10.5.zip(不要解压)2.Help3.Install New Software,如下图,N ...

  9. HttpUrlConnection的setDoOutput与setDoInput的区别

    httpUrlConnection.setDoOutput(true) httpUrlConnection.setDoInput(true) 这两个方法在develope的httpUrlConnect ...

  10. 原 nc在centos7上的安装和简单使用

    https://blog.csdn.net/qq_16414307/article/details/50291341 https://www.cnblogs.com/rocky-AGE-24/p/69 ...