HDU 4913(Least common multiple-线段树+容斥)

来源:互联网 发布:手机期货模拟软件 编辑:程序博客网 时间:2024/06/10 00:06

Least common multiple

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 767    Accepted Submission(s): 257


Problem Description
bobo has an integer set S={x1,x2,…,xn}, where xi=2ai * 3bi.

For each non-empty subsets of S, bobo added the LCM (least common multiple) of the subset up. Find the sum of LCM modulo (109+7).
 

Input
The input consists of several tests. For each tests:

The first line contains n (1≤n≤105). Each of the following n lines contain 2 integers ai,bi (0≤ai,bi≤109).
 

Output
For each tests:

A single integer, the value of the sum.
 

Sample Input
20 11 031 22 11 2
 

Sample Output
11174
 

Author
Xiaoxu Guo (ftiasch)
 

Source
2014 Multi-University Training Contest 5
 

Recommend
We have carefully selected several similar problems for you:  5426 5425 5424 5423 5422 
 




按Ai从小到大,Bi从小到大加入,考虑加入的数对答案的影响

b<Bi有cnt个 故 




#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<functional>#include<iostream>#include<cmath>#include<cctype>#include<ctime>#include<vector>using namespace std;#define For(i,n) for(int i=1;i<=n;i++)#define Fork(i,k,n) for(int i=k;i<=n;i++)#define Rep(i,n) for(int i=0;i<n;i++)#define ForD(i,n) for(int i=n;i;i--)#define RepD(i,n) for(int i=n;i>=0;i--)#define Forp(x) for(int p=pre[x];p;p=next[p])#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  #define Lson (o<<1)#define Rson ((o<<1)+1)#define MEM(a) memset(a,0,sizeof(a));#define MEMI(a) memset(a,127,sizeof(a));#define MEMi(a) memset(a,128,sizeof(a));#define MEM2(a,i) memset(a,i,sizeof(a));#define INF (2139062143)#define F (1000000007)#define MAXN (100000+10)#define fi first#define se second#define mp make_pairtypedef __int64 ll;ll mul(ll a,ll b){return (a%F*b%F)%F;}ll add(ll a,ll b){return (a+b)%F;}ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}void upd(ll &a,ll b){a=(a%F+b%F)%F;}ll pow2(ll a,ll b){if (b==0) return 1;if (b==1) return a;ll p = pow2(a,b/2);p=p*p%F;if ( b & 1 ) p = p * a%F;return p;} class SegmentTree  {      ll a[MAXN*4],sumv[MAXN*4],a2[MAXN*4],sumv2[MAXN*4];      int mark[MAXN*4];    int n;  public:      SegmentTree(){MEM(a) MEM(sumv) MEM(a2) MEM(sumv2) MEM(mark) }      SegmentTree(int _n):n(_n){MEM(a) MEM(sumv) MEM(a2) MEM(sumv2) MEM(mark)  }  void mem(int _n)      {          n=_n;          MEM(a) MEM(sumv) MEM(a2) MEM(sumv2) MEM(mark)     }      void maintain(int o,int L,int R)      {    if (L<R) {sumv[o]=(sumv[Lson]+sumv[Rson])%F;sumv2[o]=(sumv2[Lson]+sumv2[Rson])%F;}      }int y1,y2;ll v;void update(int o,int L,int R) //y1,y2,v{if (L==R) {sumv[o]++; sumv2[o]=(sumv2[o]+v)%F;return ;}else{pushdown(o);int M=(R+L)>>1;if (y1<=M) update(Lson,L,M); else update(Rson,M+1,R); }maintain(o,L,R); }void update2(int o,int L,int R) {if (y1<=L&&R<=y2) {mark[o]++;sumv2[o]=(2*sumv2[o])%F;return;}else{pushdown(o);int M=(R+L)>>1;if (y1<=M) update2(Lson,L,M); if (M< y2) update2(Rson,M+1,R); }maintain(o,L,R); }void pushdown(int o) { if (mark[o]){ mark[Lson]+=mark[o];mark[Rson]+=mark[o];ll t=pow2(2,mark[o]);sumv2[Lson]=sumv2[Lson]*t%F;sumv2[Rson]=sumv2[Rson]*t%F;mark[o]=0;}}ll _sum,_sum2; void query2(int o,int L,int R){if (y1<=L&&R<=y2){upd(_sum,sumv[o]);upd(_sum2,sumv2[o]);return;} else {pushdown(o);int M=(L+R)>>1;if (y1<=M) query2(Lson,L,M);if (M< y2) query2(Rson,M+1,R);}}void add(int l,ll v){y1=y2=l;this->v=v;update(1,1,n);}void mul(int l,int r){if (l>r) return ; y1=l,y2=r;update2(1,1,n);}ll ask(int l,int r,int b=1){if (l>r) return 0; _sum=_sum2=0;y1=l,y2=r;query2(1,1,n);if (b==1) return _sum;return _sum2;}void print(){For(i,n)cout<<ask(i,i,2)<<' ';cout<<endl;}}S;  int n;pair<ll,ll> p[MAXN];ll bb[MAXN];int main(){//freopen("hdu4913.in","r",stdin);//freopen(".out","w",stdout);while(scanf("%d",&n)==1) {int a,b;For(i,n) scanf("%d%d",&a,&b),p[i]=mp(a,b),bb[i]=p[i].se;sort(p+1,p+1+n);sort(bb+1,bb+1+n);int m=unique(bb+1,bb+1+n)-(bb+1);S.mem(m); ll ans = 0;For(i,n) {ll a=p[i].fi,b=p[i].se;ll now=pow2(2LL,a),now3=pow2(3LL,b);ll nowv=mul(now,now3);int pos = lower_bound( bb+1 , bb+1+m , p[i].se ) - (bb);ll cnt=S.ask( 1,pos,1);ll tmp=pow2(2,cnt);upd( ans , nowv * tmp % F );upd( ans , now * S.ask(pos+1,m,2) % F);S.mul(pos+1,m);S.add(pos,mul(now3,tmp));} printf("%I64d\n",ans%F); }return 0;}



0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 锅的手柄烧坏了怎么办 鞋子上的铆钉生锈了怎么办 包包上的铆钉生锈了怎么办 凉鞋的铆钉生锈了怎么办 扇子的铆钉坏了怎么办 包包的铆钉坏了怎么办 汽车半轴螺丝母拧不动怎么办? 卫衣袖子短了怎么办 u型导轨蚊帐下垂怎么办 100的水管螺纹出漏水怎么办 吊顶螺丝没有防锈处理怎么办 膨胀螺丝洞松了怎么办 膨胀螺丝眼大了怎么办 墙上螺丝孔大了怎么办 膨胀螺丝孔深了怎么办 克霉膨胀栓的线怎么办 摩托车排气管螺丝断了怎么办 汽车轮胎螺丝卸不下来怎么办 内六角螺丝卸不下来怎么办 洗衣机六角螺丝卸不动怎么办 黄油嘴打不进去怎么办 螺杆冷水机氟系统有空气怎么办 脚踏式加油枪皮碗不下去怎么办? 自攻螺丝滑丝怎么办? 大工打小工老板不管怎么办 虾缸的过滤吸虾怎么办 加热棒坏了鱼怎么办 钢材软打孔断钻头怎么办 空调余额下水管检查口按不上怎么办 风机盘管噪音大怎么办 混凝土水泥放少了怎么办 门式钢梁端板连接下料短啦怎么办? 灌桩导管堵了怎么办 公路车尾钩歪了怎么办 铃木羚羊车大灯不亮怎么办 玻璃瓶打碎了里面食物怎么办 玻璃门上轴坏了怎么办 配筋面积小了怎么办 ps大文件存不了怎么办 挑架钢丝绳拉环未预埋怎么办 出现偏拉的梁怎么办