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 ...
随机推荐
- HTTP协议 (六) 状态码详解
HTTP协议 (六) 状态码详解 HTTP状态码,我都是现查现用. 我以前记得几个常用的状态码,比如200,302,304,404, 503. 一般来说我也只需要了解这些常用的状态码就可以了. 如果 ...
- 安装repcached,并且测试其双向复制是否成功
备注:本实验不仅包括了repcached,还包括了memcache的配置安装 1.1实验环境. 1.2环境准备. 1.3配置一个memcache. 1.3.1安装memcache. 1.3.2启动me ...
- Socket客户端/服务端简单实例
1.client端 package demo.socket; import java.io.BufferedReader;import java.io.IOException;import java. ...
- php课程---JavaScript与Jquery的区别
使用Jquery必须在页面内引入一个Jquery包 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...
- asp.net identity 2.2.0 中角色启用和基本使用(五)
建立控制器UsersAdminController 第一步:在controllers文件夹上点右键>添加>控制器, 我这里选的是“MVC5 控制器-空”,名称设置为:UsersAdminC ...
- php对二维数组进行相关操作(排序、转换、去空白等)
php对二维数组进行相关操作(排序.转换.去空白等) 投稿:lijiao 字体:[增加 减小] 类型:转载 时间:2015-11-04 这篇文章主要介绍了php对二维数组进行相关操作,包括php对 ...
- C#常用命名空间
MSDN上的C#.NET Framework类库文档目录树,本人觉得有点不得要领,于是参考搜到的结果简单整理如下: 一.基础命名空间 System 处理内建数据.数学计算.随机数的产生.环境变量.垃圾 ...
- DotNetBar中ListViewEx控件的使用
最近一直在学习DotNetBar,今天遇到的问题是ListView的使用问题,其实没有特别难的,只是写在这里给自己留个记录. 首先,在Form中添加一个ListViewEx控件, 初始化中写代码如下: ...
- 带你玩转JavaWeb开发之一 - HTML快速入门
一,html简介 1,html是什么 Html是用来描述网页的一种语言. (1)HTML 指的是超文本标记语言 (Hyper Text Markup Language) (2)HTML 不是一种编程语 ...
- 【翻译】How To Tango With Django 1.5.4 第三章
django基础 3.1测试你的配置 测试你的python版本和你的django版本是否兼容 3.2新建django工程 在dos里面进到你事先新建的code文件夹,然后在执行下列命令新建工程 c:\ ...