hdu 4407 容斥原理

Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1726    Accepted Submission(s): 486


Problem Description
XXX is puzzled with the question below:

1, 2, 3, ..., n (1<=n<=400000) are placed in a line. There are m (1<=m<=1000) operations of two kinds.

Operation 1: among the x-th number to the y-th number (inclusive), get the sum of the numbers which are co-prime with p( 1 <=p <= 400000).
Operation 2: change the x-th number to c( 1 <=c <= 400000).

For each operation, XXX will spend a lot of time to treat it. So he wants to ask you to help him.
 

Input
There are several test cases.
The first line in the input is an integer indicating the number of test cases.
For each case, the first line begins with two integers --- the above mentioned n and m.
Each the following m lines contains an operation.
Operation 1 is in this format: "1 x y p".
Operation 2 is in this format: "2 x c".
 

Output
For each operation 1, output a single integer in one line representing the result.
 

Sample Input
  
  
1 3 3 2 2 3 1 1 3 4 1 2 3 6
 

Sample Output
  
  
7 0
 

Source
 


一开始是1-400000的序列,两种操作,一种是把第x个数改为y,另一种是计算从第x个数开始,到第y个数所有数里与p互素的元素的和。

由于序列很长,修改非常少,只有1000次,可以把修改的位置保存下来,对区间用容斥原理计算,然后再根据修改过的数,进行单点处理,从而得到答案。

代码:

/* ***********************************************
Author :rabbit
Created Time :2014/4/2 14:21:05
File Name :13.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
typedef __int64 ll;
bool isprime[1001000];
ll prime[1001000],factor[1010];
void getprime(){
    memset(isprime,1,sizeof(isprime));isprime[1]=0;
    ll &cnt=prime[0];cnt=0;
    for(ll i=2;i<=1000000;i++){
        if(isprime[i])prime[++cnt]=i;
        for(ll j=1;j<=cnt&&i*prime[j]<=1000000;j++){
            isprime[i*prime[j]]=0;
            if(i%prime[j]==0)break;
        }
    }
//    cout<<prime[0]<<endl;
}
void getfactor(ll x){
    ll &cnt=factor[1000];cnt=0;
    for(ll i=1;i<=prime[0]&&prime[i]*prime[i]<=x;i++)
        if(x%prime[i]==0){
            factor[cnt++]=prime[i];
            while(x%prime[i]==0)x/=prime[i];
        }
    if(x>1)factor[cnt++]=x;
}
ll sum[500100];
ll cal(ll x){
    ll cnt=factor[1000],ans=0;
    for(ll i=1;i<(1<<cnt);i++){
        ll pp=1,ss=0;
        for(ll j=0;j<cnt;j++)
            if(i&(1<<j)){
                ss++;pp*=factor[j];
            }
		ll d=x/pp;
		d=d*(d+1)*pp/2;
        if(ss%2)ans+=d;
        else ans-=d;
    }
    return sum[x]-ans;
}
ll gcd(ll x,ll y){
	if(x==0)return y;
	return gcd(y%x,x);
}
int main()
{
     //freopen("data.in","r",stdin);
     //freopen("data.out","w",stdout);
     getprime();
     int T;
	 sum[1]=1;for(ll i=2;i<=400000;i++)sum[i]=sum[i-1]+i;
     scanf("%d",&T);
     while(T--){
        ll N,M;
		scanf("%I64d%I64d",&N,&M);
		ll x,y,p,op;
		ll ans=0;
		map<ll,ll> mp;
		map<ll,ll>::iterator it;
		//getfactor(3);cout<<cal(3)<<endl;
		while(M--){
			scanf("%I64d",&op);
			if(op==2){
				scanf("%I64d%I64d",&x,&y);
				mp[x]=y;
			}
			else{
				scanf("%I64d%I64d%I64d",&x,&y,&p);
				getfactor(p);
				ll ans=cal(y)-cal(x-1);
				for(it=mp.begin();it!=mp.end();it++)
					if(it->first>=x&&it->first<=y){
						if(gcd(it->first,p)==1)ans-=it->first;
						if(gcd(it->second,p)==1)ans+=it->second;
					}
				cout<<ans<<endl;
			}
		}
	//	for(it=mp.begin();it!=mp.end();it++)cout<<it->first<<" "<<(*it).second<<endl;
     }
     return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值