首页 > 代码库 > BZOJ 3813 奇数国 线段树+数论

BZOJ 3813 奇数国 线段树+数论

题目大意:给定一个序列,每个数都由60个最小的素数的乘积构成,求某段的乘积的欧拉函数值对19961993取模后的值,支持单点修改

19961993是个质数 出题人还是满贴心的

利用线段树维护乘积取模后的值以及哪些素数出现过 后者用bitset维护

得到的值根据bitset里出现过的素数来计算欧拉函数值

时间复杂度O(nlog10W+60n)

#include <bitset>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MOD 19961993
#define M 100100
using namespace std;
const int prime[60]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281};
const int rev[]={9980997,13307996,7984798,11406854,14517814,18426456,9393880,5253157,16490343,8260136,2575742,18343454,3895024,17640832,1698894,3013132,7443456,4581442,9236147,18275065,6562848,2779519,7936697,4037258,6379607,19566707,13566404,4104336,3662752,13602421,16661192,1219054,13259427,9047523,3751248,8196316,14621843,1714528,12192356,11884887,8029406,13455046,17976246,13342473,14084859,15548287,10217514,9846724,5364237,3486812,1627803,14950615,1076789,12406658,19340609,8652728,7791857,7955334,1657495,8808852};
struct Segtree{
	Segtree *ls,*rs;
	bitset<60>cnt;
	long long product;
	void* operator new (size_t)
	{
		static Segtree mempool[M<<1],*C=mempool;
		return C++;
	}
	void Build_Tree(int x,int y)
	{
		int mid=x+y>>1;
		if(x==y)
		{
			cnt[1]=1;
			product=3;
			return ;
		}
		ls=new Segtree;rs=new Segtree;
		ls->Build_Tree(x,mid);
		rs->Build_Tree(mid+1,y);
		cnt=ls->cnt|rs->cnt;
		product=ls->product*rs->product%MOD;
	}
	void Modify(int x,int y,int pos,int val)
	{
		int mid=x+y>>1;
		if(x==y)
		{
			int i;
			for(i=0;i<60;i++)
				if(val%prime[i]==0)
					cnt[i]=1;
				else
					cnt[i]=0;
			product=val;
			return ;
		}
		if(pos<=mid) ls->Modify(x,mid,pos,val);
		else rs->Modify(mid+1,y,pos,val);
		cnt=ls->cnt|rs->cnt;
		product=ls->product*rs->product%MOD;
	}
	pair<bitset<60>,long long> Query(int x,int y,int l,int r)
	{
		int mid=x+y>>1;
		if(x==l&&y==r)
			return make_pair(cnt,product);
		if(r<=mid) return ls->Query(x,mid,l,r);
		if(l>mid) return rs->Query(mid+1,y,l,r);
		pair<bitset<60>,long long> temp1=ls->Query(x,mid,l,mid);
		pair<bitset<60>,long long> temp2=rs->Query(mid+1,y,mid+1,r);
		return make_pair(temp1.first|temp2.first,temp1.second*temp2.second%MOD);
	}
}*root=new Segtree;
int m;
int main()
{
	int i,j,p,x,y;
	cin>>m;
	root->Build_Tree(1,100000);
	for(i=1;i<=m;i++)
	{
		scanf("%d%d%d",&p,&x,&y);
		if(p==1)
			root->Modify(1,100000,x,y);
		else
		{
			pair<bitset<60>,long long> temp=root->Query(1,100000,x,y);
			long long ans=temp.second;
			for(j=0;j<60;j++)
				if(temp.first[j])
					ans*=rev[j],ans%=MOD;
			printf("%lld\n",ans);
		}
	}
	return 0;
}


BZOJ 3813 奇数国 线段树+数论