HDU 4947 GCD Array 容斥原理+树状数组
GCD Array
Time Limit: 11000/5500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 843    Accepted Submission(s):
205
function can be reduced to the following problem:
Maintain an array a
with index from 1 to l. There are two kinds of operations:
1. Add v to
ax for every x that gcd(x,n)=d.
  2. Query 
 
0".
For each test case, the first line contains two integers
l,Q(1<=l,Q<=5*10^4), indicating the length of the array and the number of
the operations.
In following Q lines, each line indicates an operation,
and the format is "1 n d v" or "2 x"
(1<=n,d,v<=2*10^5,1<=x<=l).
case number counting from 1.
Then output the answer to each query.
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
using namespace std;
typedef __int64 LL; const int maxn = +;
const int INF = 2e5+;
LL p[maxn];
bool s[INF];
int prime[],len; void Init()
{
len = ;
memset(s,false,sizeof(s));
for(int i=;i<INF;i++)
{
if(s[i]==true)continue;
prime[++len] = i;
for(int j=i+i;j<INF;j=j+i)
s[j]=true;
}
}
void add(int x,int n,int num1)
{
for(int i=x;i<=n;i=i+(i&(-i)))
p[i] = p[i] + num1;
}
LL query(int x)
{
if(x==)return ;
LL sum1 = ;
while(x)
{
sum1=sum1+p[x];
x=x-(x&(-x));
}
return sum1;
}
int Q[],yz[],ylen,qlen;
void init(int n)
{
ylen = qlen = ;
for(int i=;prime[i]*prime[i]<=n;i++)
{
if(n%prime[i]==)
{
while(n%prime[i]==) n=n/prime[i];
yz[++ylen] = prime[i];
}
}
if(n!=) yz[++ylen] = n;
Q[]=-;
for(int i=;i<=ylen;i++)
{
int k = qlen;
for(int j=;j<=k;j++)
Q[++qlen] = -*Q[j]*yz[i];
}
}
int main()
{
int n,m,hxl,d,v,size1,x,T=;
Init();
while(scanf("%d%d",&n,&m)>)
{
if(n==&&m==)break;
memset(p,,sizeof(p));
printf("Case #%d:\n",++T);
while(m--)
{
scanf("%d",&size1);
if(size1==)
{
scanf("%d%d%d",&hxl,&d,&v);
if(hxl%d!=)continue;
hxl = hxl /d;
int tom = n/d;
add(d,n,v);
init(hxl);
for(int i=;i<=qlen;i++)
if(Q[i]<) {
Q[i] = -Q[i];
if(Q[i]>tom)continue;
add(Q[i]*d,n,v);
}
else {
if(Q[i]>tom)continue;
add(Q[i]*d,n,-v);
}
}
else{
scanf("%d",&x);
LL sum1 = ;
for(int i=,la=;i<=x;i=la+){
la = x/(x/i);
sum1 = sum1 + (query(la)-query(i-))*(x/i);
}
printf("%I64d\n",sum1);
}
}
}
return ;
}
HDU 4947 GCD Array 容斥原理+树状数组的更多相关文章
- HDU 4777 Rabbit Kingdom --容斥原理+树状数组
		
题意: 给一个数的序列,询问一些区间,问区间内与区间其他所有的数都互质的数有多少个. 解法: 直接搞有点难, 所谓正难则反,我们求区间内与其他随便某个数不互质的数有多少个,然后区间长度减去它就是答案了 ...
 - HDU 5862 Counting Intersections(离散化+树状数组)
		
HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...
 - hdu 5517 Triple(二维树状数组)
		
Triple Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
 - HDU 5869 Different GCD Subarray Query (GCD种类预处理+树状数组维护)
		
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5869 问你l~r之间的连续序列的gcd种类. 首先固定右端点,预处理gcd不同尽量靠右的位置(此时gc ...
 - HDU 5869 Different GCD Subarray Query 树状数组+离线
		
Problem Description This is a simple problem. The teacher gives Bob a list of problems about GCD (Gr ...
 - HDU 5869 Different GCD Subarray Query  树状数组 + 一些数学背景
		
http://acm.hdu.edu.cn/showproblem.php?pid=5869 题意:给定一个数组,然后给出若干个询问,询问[L, R]中,有多少个子数组的gcd是不同的. 就是[L, ...
 - HDU 5792   L - World is Exploding 。容斥原理 + 树状数组 + 离散化
		
题目,要求找出有多少对这样的东西,四个数,并且满足num[a]<num[b] &&num[c]>num[d] 要做这题,首先要懂得用树状数组,我设,下面的小于和大于都是严格 ...
 - HDU 1394 Minimum Inversion Number   (  树状数组求逆序数    )
		
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number ...
 - HDU 5862 Counting Intersections (树状数组)
		
Counting Intersections 题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 Description Given ...
 
随机推荐
- Ubuntu 12.04 系统安装极点五笔输入法
			
习惯用五笔了,在Ubuntu下安装了下五笔: 下面就实际操作,安装极点五笔输入法! 在终端中执行如下命令: sudo wget http://www.xiit.cn/wp-content/upload ...
 - 一个参数大小写引发的uploadify报错 "Syntax error, unrecognized expression: #"
			
上传控件uploadify 报错"Syntax error, unrecognized expression: #" 版本为 uploadify3.2 报错原因:参数ID[hi ...
 - VPN fq工具的选择
			
豆荚VPN还是不错的.有时候百度会打不开,重新连接一下就可以了 http://wandou.shouyo99.com/ 如果高速模式不可以,请记得选择PPTP模式!!!但有个副作用就是百度打不开了--
 - 测试的程序 test.php,保存放IIS的根目录下
			
IIS+PHP的配置的方法,试过之后很多都不能达到效果.于是总结了大部分的文章后就得出了这样的方法 一.下载必须的程序:(1) 先到PHP的官方网站下载一个PHP(本文就以PHP 4.4.2为例).网 ...
 - find exec 运用
			
实例1:ls -l命令放在find命令的-exec选项中 : find . -type f -exec ls -l {} \; 实例2:在目录中查找更改时间在n日以前的文件并删除它们: find . ...
 - 浅谈scrum站立会议
			
什么是每日站立会议? 站立会议是让团队成员每日面对面站立互相交流他们所承担任务的进度.它的一个附带好处是让领导或经理能了解到工作情况.但本质上是为了团队交流,不是报告会议! 为什么开展每日 ...
 - String-原型属性
			
<script> /*将trim方法定义到字符串对象中 *使用字符串的原型属性来完成 *原型prototype:就是该对象的一个描述,该描述中如果添加新功能,那么该对象就具备这些新功能. ...
 - 对蓝牙profile的理解
			
蓝牙profile协议概览.pdf 之所以把Profile翻译为配置文件,是为避免和JavaME中的简表混淆.配置文件也是蓝牙 SIG官方网站给出的标准翻译. 想要使用蓝牙无线技术,设备必须能够翻译特 ...
 - JS中的事件冒泡(Bubble)和事件捕获(capture)以及如何阻止事件的冒泡
			
对“捕获”和“冒泡”这两个概念,通常我们对冒泡了解和使用的会更多一些,因为在我们使用的所有浏览器中,都支持事件冒泡 ,即事件由子元素向祖先元素传播的,就 像气泡从水底向水面上浮一样.而在像firefo ...
 - 小谈pointer和relation
			
在apicloud的数据库中,pointer和relation是在很难让人理解. 通过不断的实践,终于有点明白了. pointer和relation作用:在nosql数据库中实现表之间的关联 首先来说 ...