Gcd & Lcm game
来源:互联网 发布:听书软件电脑版 编辑:程序博客网 时间:2024/06/05 07:29
Gcd & Lcm game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 978 Accepted Submission(s): 217
Problem Description
Tired of playing too much computer games, alpc23 is planning to play a game on numbers. Because plus and subtraction is too easy for this gay, he wants to do some gcd and lcm operations in a number sequence. After playing it a few times, he has found it is also too boring. So he plan to do a more challenge job: he wants to change several numbers in this sequence and also work out the gcd or lcm of all the number in a subsequence of the whole sequence.
To be a friend of this gay, you have been invented by him to play this interesting game with him. Of course, you need to work out the answers faster than him to get a free lunch, He he…
To be a friend of this gay, you have been invented by him to play this interesting game with him. Of course, you need to work out the answers faster than him to get a free lunch, He he…
Input
There are multiple test cases.
For each test case.The first line is the length of sequence n, and the number of queries q. (1<=n, q<=100000) The second line has n numbers, they are the initial n numbers of the sequence a1,a2, …,an,
From the third line to the q+2 line are the description of the q operations. They are the one of the two forms:
L k1 k2 p; you need to work out the value after mod p of lcm of the subsequence from k1 to k2, inclusive. (1<=k1<=k2<=n)
G k1 k2 p; you need to work out the value after mod p of gcd of the subsequence from k1 to k2, inclusive. (1<=k1<=k2<=n)
C k v; the k-th number of the sequence has been changed to v.
You can assume that all the numbers before and after the replacement are positive and no larger than 100.
For each test case.The first line is the length of sequence n, and the number of queries q. (1<=n, q<=100000) The second line has n numbers, they are the initial n numbers of the sequence a1,a2, …,an,
From the third line to the q+2 line are the description of the q operations. They are the one of the two forms:
L k1 k2 p; you need to work out the value after mod p of lcm of the subsequence from k1 to k2, inclusive. (1<=k1<=k2<=n)
G k1 k2 p; you need to work out the value after mod p of gcd of the subsequence from k1 to k2, inclusive. (1<=k1<=k2<=n)
C k v; the k-th number of the sequence has been changed to v.
You can assume that all the numbers before and after the replacement are positive and no larger than 100.
Output
For each of the first or second operation, you need to output the answer in each line.
Sample Input
6 41 2 4 5 6 3L 2 5 17G 4 6 4C 4 9G 4 6 4
Sample Output
913
ps其实这道题,如果朴素的做法做的话,不难想到,但是由于时间卡的很紧,只能用状态压缩的方法(压缩成int然后用位运算)来加快运算速度
T了好几 发,也没想到怎么搞,最后还看大佬博客才会的,很巧妙。
除了状态转化外,其他的就是朴素的线段树了。
#include <bits/stdc++.h>using namespace std;typedef long long LL;const int INF = 0x3f3f3f3f;const int maxn = 100000+10;int prime[]={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};int dpos[]={28,25,23,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0};int a[]={1,2,4,8,16,32,64};int b[]={1,3,9,27,81};int c[]={1,5,25};int d[]={1,7,49};int num[maxn],N,M;struct node{ int l,r; int Data_lcm,Data_gcd;}Nodes[maxn<<2];inline int change_input_to_int(int x){ int cnt,y; y = 0; for(int i = 0; i < 25 && x>1; i++){ cnt = 0; while(x%prime[i] == 0){ x/=prime[i]; cnt++; } y |= cnt<<dpos[i]; } return y;}inline int change_int_to_ans(int x,int p){ LL y = 1; int k; k = x>>dpos[0];y = y*a[k]%p;x = x^(k<<dpos[0]); k = x>>dpos[1];y = y*b[k]%p;x = x^(k<<dpos[1]); k = x>>dpos[2];y = y*c[k]%p;x = x^(k<<dpos[2]); k = x>>dpos[3];y = y*d[k]%p;x = x^(k<<dpos[3]); for(int i = 4; i < 25; i++) if(x & (1<<dpos[i])) y = y*prime[i]%p; return y;}inline int max_lcm(int x,int y){ int ans = 0; ans |= max(x&0x70000000,y&0x70000000); ans |= max(x&0x0e000000,y&0x0e000000); ans |= max(x&0x01800000,y&0x01800000); ans |= max(x&0x00600000,y&0x00600000); ans |= ((x&0x001fffff)|(y&0x001fffff)); return ans;}inline int min_gcd(int x,int y){ int ans = 0; ans |= min(x&0x70000000,y&0x70000000); ans |= min(x&0x0e000000,y&0x0e000000); ans |= min(x&0x01800000,y&0x01800000); ans |= min(x&0x00600000,y&0x00600000); ans |= ((x&0x001fffff)&(y&0x001fffff)); return ans;}inline void update_father(int now){ Nodes[now].Data_lcm = max_lcm(Nodes[now<<1].Data_lcm,Nodes[now<<1|1].Data_lcm); Nodes[now].Data_gcd = min_gcd(Nodes[now<<1].Data_gcd,Nodes[now<<1|1].Data_gcd);}void segtree_build(int now,int l,int r){ Nodes[now].l = l; Nodes[now].r = r; if(l == r){ Nodes[now].Data_lcm = Nodes[now].Data_gcd = change_input_to_int(num[l]); return; } int mid = (l+r)>>1; segtree_build(now<<1,l,mid); segtree_build(now<<1|1,mid+1,r); update_father(now);}void segtree_update(int now,int pos,int val){ if(Nodes[now].l == Nodes[now].r){ Nodes[now].Data_lcm = Nodes[now].Data_gcd = change_input_to_int(val); return; } int mid = (Nodes[now].l+Nodes[now].r)>>1; if(pos <= mid) segtree_update(now<<1,pos,val); else segtree_update(now<<1|1,pos,val); update_father(now);}int segtree_query(int now,int x,int y,char ch){ int res; if(ch == 'L'){ res = 0; if(x <= Nodes[now].l && Nodes[now].r <= y){ return Nodes[now].Data_lcm; } int mid = (Nodes[now].l+Nodes[now].r)>>1; if(x <= mid) res = max_lcm(res,segtree_query(now<<1,x,y,ch)); if(y > mid) res = max_lcm(res,segtree_query(now<<1|1,x,y,ch)); } else{ res = 0x7fffffff; if(x <= Nodes[now].l && Nodes[now].r <= y){ return Nodes[now].Data_gcd; } int mid = (Nodes[now].l+Nodes[now].r)>>1; if(x <= mid) res = min_gcd(res,segtree_query(now<<1,x,y,ch)); if(y > mid) res = min_gcd(res,segtree_query(now<<1|1,x,y,ch)); } return res;}int main(){ char ch[5]; int x,y,id,val,p; while(~scanf("%d %d",&N,&M)){ for(int i = 1; i <= N; i++) scanf("%d",&num[i]); segtree_build(1,1,N); while(M--){ scanf("%s",ch); if(ch[0] == 'L' || ch[0] == 'G'){ scanf("%d %d %d",&x,&y,&p); int result = segtree_query(1,x,y,ch[0]); printf("%d\n",change_int_to_ans(result,p)); } else{ scanf("%d %d",&id,&val); segtree_update(1,id,val); } } } return 0;}
阅读全文
0 0
- Gcd & Lcm game
- hdu 3071 Gcd & Lcm game
- HDU 3071 Gcd & Lcm game
- HDU 3071 Gcd & Lcm game (线段树+素因子分解)
- gcd,lcm
- gcd & lcm
- LCM GCD
- GCD LCM
- lcm/gcd
- gcd & lcm
- GCD LCM
- 【GCD&LCM】
- gcd && lcm
- HDU 3071-Gcd & Lcm game-线段树+素因子分解-[解题报告]HOJ
- gcd,lcm 汇编版本
- gcd,lcm 汇编版本
- zoj 1577 GCD & LCM
- ZOJ 1577 GCD & LCM
- JPA 开发中遇到的错误
- GoldenGate错误
- 冒泡排序
- Linux下安装GraphicsMagick
- 类加载的过程
- Gcd & Lcm game
- Android笔记(六)ANR
- 计算机组成原理知识点(第七章: 指令系统)
- linux c++ 多进程 匿名管道 命名管道 消息队列 信号量 共享内存 内存映射
- Ubuntu16.10 下codeblocks的安装配置
- 在(Docker里的)Jenkins里运行Docker
- 同时集成阿里云旺与友盟推送,初始化失败s:-11,s1:accs bindapp error!的解决办法
- 决策树
- 走向云计算之MapReduce应用案例详解