GCD Array

Time Limit: 11000/5500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 843    Accepted Submission(s):
205

Problem Description
Teacher Mai finds that many problems about arithmetic
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

 
Input
There are multiple test cases, terminated by a line "0
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).

 
Output
For each case, output "Case #k:" first, where k is the
case number counting from 1.

Then output the answer to each query.

 
Sample Input
6 4
1 4 1 2
2 5
1 3 3 3
2 3
0 0
 
Sample Output
 
Case #1:
6
7
 
Author
xudyh
 
Source
 
 题意:2个操作
         在长度长度为len的数组操作
         1 n v d 给数组x下标满足 gcd(x,n)=d的对应位置+v.
         2 x 询问数组sum(a1,,,ax)的和
思路:如果一个一个更新是会超时的。
        方法大致是这样,首先和GCD()问题类似,从反面着手,首先全部数字+v,然后在gcd(x,n)!=d的位置-v。
        gcd(x,n) = d 可以转化为gcd(x/d,n/d)=1; 转化为求互质。这个应该容易理解些。
        现在我设立一个数组a[ ] ,a[i] 代表的意思是 gcd(x,n)为i的倍数的时候的方案数。
    那么原来的问题,N = n/d ,现在就分解N的素因子,并进行容斥。比如6 : 2 , 3 , -6.
        首先在所有的数字上加v,然后用容斥的结果在数组上进行对不互质的地方进行-v。
        举一个例子。
    比如len = 10 , gcd(x,6) = 2; ==> gcd(x/2,3)=1;
       那么我们只能在[1-10/2]进行更新。
       求得的容斥结果为 2 , 3, -6.那么首先我们在所有的数字上+v,就是a[d]=v,(a[2]=v);
       然后在2*d上减去v,3*d上减去v,6*d上加上v。就是这样。
       至于为什么要*d,这个思考一下就会知道的。
       这就是第一个操作了。
       第二个操作就是询问数组【1-x】的和,转化为求数组在a[i]有多少个 x/i 个。
       就是sum(a[i]*x/i) ;由于 x/i 我们能用sqrt()的算法来做。这里也就牵涉到求和,用树状数组。
       代码比较挫,几乎是压线过。
      
 #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 容斥原理+树状数组的更多相关文章

  1. HDU 4777 Rabbit Kingdom --容斥原理+树状数组

    题意: 给一个数的序列,询问一些区间,问区间内与区间其他所有的数都互质的数有多少个. 解法: 直接搞有点难, 所谓正难则反,我们求区间内与其他随便某个数不互质的数有多少个,然后区间长度减去它就是答案了 ...

  2. HDU 5862 Counting Intersections(离散化+树状数组)

    HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...

  3. hdu 5517 Triple(二维树状数组)

    Triple Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  4. HDU 5869 Different GCD Subarray Query (GCD种类预处理+树状数组维护)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5869 问你l~r之间的连续序列的gcd种类. 首先固定右端点,预处理gcd不同尽量靠右的位置(此时gc ...

  5. HDU 5869 Different GCD Subarray Query 树状数组+离线

    Problem Description This is a simple problem. The teacher gives Bob a list of problems about GCD (Gr ...

  6. HDU 5869 Different GCD Subarray Query 树状数组 + 一些数学背景

    http://acm.hdu.edu.cn/showproblem.php?pid=5869 题意:给定一个数组,然后给出若干个询问,询问[L, R]中,有多少个子数组的gcd是不同的. 就是[L, ...

  7. HDU 5792 L - World is Exploding 。容斥原理 + 树状数组 + 离散化

    题目,要求找出有多少对这样的东西,四个数,并且满足num[a]<num[b] &&num[c]>num[d] 要做这题,首先要懂得用树状数组,我设,下面的小于和大于都是严格 ...

  8. HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number                         ...

  9. HDU 5862 Counting Intersections (树状数组)

    Counting Intersections 题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 Description Given ...

随机推荐

  1. Socket客户端/服务端简单实例

    1.client端 package demo.socket; import java.io.BufferedReader;import java.io.IOException;import java. ...

  2. Exception在方法继承中的限制

    问题: 父类或接口中定义了方法应该抛出的异常,那么当子类重写方法时应该怎样定义将要抛出的异常: 首先定义异常和父类: class Exception1 extends Exception {} cla ...

  3. SQL Server 存储过程(转)

    Transact-SQL中的存储过程,非常类似于Java语言中的方法,它可以重复调用.当存储过程执行一次后,可以将语句缓存中,这样下次执行的时候直接使用缓存中的语句.这样就可以提高存储过程的性能. Ø ...

  4. IOS第14天(2, Modal控制)

    ******控制器modal - (void)btnClick { // 创建控制器对象 HMJumpViewController *jump = [[HMJumpViewController all ...

  5. maven使用本地jar包

    引入本地jar包 方式一:将本地Jar包安装到本地仓库,再按常规方式引用 mvn install:install-file -Dfile=libs\tools.jar -DgroupId=com.su ...

  6. javaWeb中servlet开发(5)——WEB开发模式:Mode I与Mode II

    1.servlet开发 2.model I模式 客户端通过访问JSP,调用里面的javabean,而通过javabean调用处理数据库的操作,javabean中有专门处理数据库的操作,数据库主要以DA ...

  7. WPF部署问题 解决:The application requires that the assembly...be installed in the GAC

    vs-->引用-->找到问题类库-->邮件属性--->特定版本-->false done

  8. php实现数字格式化,数字每三位加逗号的功能函数

    原地址:http://www.jb51.net/article/73781.htm php实现数字格式化,数字每三位加逗号的功能函数,具体代码如下: ? 1 2 3 4 5 6 7 8 9 10 11 ...

  9. React学习一

    一.运行起来react 1.引入react库 引入对应的js即可,第三方cdn引入 <script src="https://cdnjs.cloudflare.com/ajax/lib ...

  10. Jmeter工作原理