【HDOJ】3071 Gcd & Lcm game
刚开始看这个题目,觉得没法做。关键点是数据小于100。因此,可以枚举所有小于100的素因子进行位压缩。
gcd就是求最小值,lcm就是求最大值。c++有时候超时,g++800ms。线段树可解。
/* 3071 */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1
#define ui32 unsigned int typedef struct {
ui32 mx, mn;
} node_t; int factor[] = {,,,,,,,,,,,,,,,,,,,,,,,,};
int width[] = {,,,,,,,,,,,,,,,,,,,,,,,,};
int shift[] = {,,,,,,,,,,,,,,, , , , , , , , , , };
int mask[] = {,,,,,,,,,,,,,,,,,,,,,,,,};
const int maxn = 1e5+;
int M[][];
node_t nd[maxn<<];
int L, R, mod; void init() {
rep(i, , ) {
M[i][] = ;
rep(j, , mask[i]+)
M[i][j] = M[i][j-] * factor[i];
}
} int getVal(int x) {
int ret = , tmp; for (int i=; i>=; --i) {
tmp = M[i][x&mask[i]];
ret = ret * tmp % mod;
x >>= width[i];
} return ret;
} ui32 calc(int x) {
ui32 ret = , tmp; rep(i, , ) {
ret <<= width[i];
tmp = ;
if (x%factor[i] == ) {
while (x%factor[i] == ) {
++tmp;
x /= factor[i];
}
}
ret += tmp;
} return ret;
} ui32 mymax(ui32 x, ui32 y) {
ui32 ret = max(x&0x70000000, y&0x70000000)\
| max(x&0x0e000000, y&0x0e000000)\
| max(x&0x01800000, y&0x01800000)\
| max(x&0x00600000, y&0x00600000)\
| ((x&0x001fffff) | (y&0x001fffff));
return ret;
} ui32 mymin(ui32 x, ui32 y) {
ui32 ret = min(x&0x70000000, y&0x70000000)\
| min(x&0x0e000000, y&0x0e000000)\
| min(x&0x01800000, y&0x01800000)\
| min(x&0x00600000, y&0x00600000)\
| ((x&0x001fffff) & (y&0x001fffff));
return ret;
} inline void PushUp(int rt) {
int lb = rt << ;
int rb = lb | ; nd[rt].mx = mymax(nd[lb].mx, nd[rb].mx);
nd[rt].mn = mymin(nd[lb].mn, nd[rb].mn);
} void Build(int l, int r, int rt) {
if (l == r) {
int x;
scanf("%d", &x);
nd[rt].mx = nd[rt].mn = calc(x);
return ;
} int mid = (l + r) >> ; Build(lson);
Build(rson);
PushUp(rt);
} ui32 Query_mx(int l, int r, int rt) {
if (L<=l && R>=r) {
return nd[rt].mx;
} int mid = (l + r) >> ;
ui32 ret; if (R <= mid) {
ret = Query_mx(lson);
} else if (L > mid) {
ret = Query_mx(rson);
} else {
ui32 ltmp = Query_mx(lson);
ui32 rtmp = Query_mx(rson);
ret = mymax(ltmp, rtmp);
} return ret;
} ui32 Query_mn(int l, int r, int rt) {
if (L<=l && R>=r) {
return nd[rt].mn;
} int mid = (l + r) >> ;
ui32 ret; if (R <= mid) {
ret = Query_mn(lson);
} else if (L > mid) {
ret = Query_mn(rson);
} else {
ui32 ltmp = Query_mn(lson);
ui32 rtmp = Query_mn(rson);
ret = mymin(ltmp, rtmp);
} return ret;
} void Update(int k, int x, int l, int r, int rt) {
if (l == r) {
nd[rt].mx = nd[rt].mn = calc(x);
return ;
} int mid = (l + r) >> ; if (k <= mid)
Update(k, x, lson);
else
Update(k, x, rson); PushUp(rt);
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int n, q;
char op[];
int tmp, k;
int ans; init();
while (scanf("%d %d", &n, &q) != EOF) {
Build(, n, );
while (q--) {
scanf("%s", op);
if (op[] == 'L') {
scanf("%d %d %d", &L, &R, &mod);
tmp = Query_mx(, n, );
ans = getVal(tmp);
printf("%d\n", ans);
} else if (op[] == 'G') {
scanf("%d %d %d", &L, &R, &mod);
tmp = Query_mn(, n, );
ans = getVal(tmp);
printf("%d\n", ans);
} else {
scanf("%d %d", &k, &tmp);
Update(k, tmp, , n, );
}
}
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}
【HDOJ】3071 Gcd & Lcm game的更多相关文章
- 【Pollard-rho算法】【DFS】poj2429 GCD & LCM Inverse
题意:给你一两个数m和n,它们分别是某对数A,B的gcd和lcm,让你求出一对使得A+B最小的A,B. n/m的所有质因子中,一定有一部分是只在A中的,另一部分是只在B中的. 于是对n/m质因子分解后 ...
- 【HDOJ】1695 GCD
莫比乌斯反演简单题目. /* 1695 */ #include <iostream> #include <string> #include <map> #inclu ...
- 【51NOD-0】1012 最小公倍数LCM
[算法]欧几里德算法 #include<cstdio> int gcd(int a,int b) {?a:gcd(b,a%b);} int main() { int a,b; scanf( ...
- 【精】iOS GCD 具体解释
一.介绍 1.什么是GCD? Grand Central Dispatch.是苹果公司开发的一套多核编程的底层API. GCD首次公布在Mac OS X 10.6,iOS4及以上也可用.GCD存在于l ...
- 【51nod】2026 Gcd and Lcm
题解 话说LOJ说我今天宜学数论= =看到小迪学了杜教筛去蹭了一波小迪做的题 标解的杜教筛的函数不懂啊,怎么推的毫无思路= = 所以写了个复杂度稍微高一点的?? 首先,我们发现f是个积性函数,那么我们 ...
- 【HDOJ】4983 Goffi and GCD
题意说的非常清楚,即求满足gcd(n-a, n)*gcd(n-b, n) = n^k的(a, b)的不同对数.显然gcd(n-a, n)<=n, gcd(n-b, n)<=n.因此当n不为 ...
- 【HDOJ】4729 An Easy Problem for Elfness
其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...
- 【数论】二进制GCD
二进制GCD GCD这种通用的算法相信每个OLER都会 ,辗转相除,代码只有四行 : int GCD(int a,int b){ if(b==0) return a; return GCD(b ...
- 【转】UVALive 5964 LCM Extreme --欧拉函数
题目大意:求lcm(1,2)+lcm(1,3)+lcm(2,3)+....+lcm(1,n)+....+lcm(n-2,n)+lcm(n-1,n)解法:设sum(n)为sum(lcm(i,j))(1& ...
随机推荐
- 2015/7/6 (!长期更新!)C语言从零——张呵呵
随即呈上! By He_He _S 小组 @成都七中高新OI2015
- Installation Phases and In-Script Execution for Custom Actions in Windows Installer
用 InstallShield 依照 Custom Action Wizard 创建 Custom Action 时,会遇到下面的几个选项: In-Script Execution Install U ...
- Ubuntu下GCC的安装以及版本控制
在Ubuntu下安装GCC和其他一些Linux系统有点不一样. 方法一: 该方法超简单:sudo apt-get build-depgcc 就上面这条命令就可以搞定 方法二:sudo apt-get ...
- (转)MySql可视化工具MySQL Workbench使用教程
转自:http://www.cnblogs.com/daimage/archive/2012/02/25/2367534.html 1. MySQL Workbench MySQL Workbench ...
- iOS 详细解释@property和@synthesize关键字
/** 注意:由@property声明的属性 在类方法中通过下划线是获取不到的 必须是通过 对象名.属性 才能获取到!- @property和@synthesize关键字是针对成员变量以及get/se ...
- 判断滚动条到底部的JS代码
这篇文章介绍了判断滚动条到底部的JS代码,有需要的朋友可以参考一下 判断滚动条到底部,需要用到DOM的三个属性值,即scrollTop.clientHeight.scrollHeight. scrol ...
- [JQuery]选择器详解
示例 说明 $(this) 当前元素 $("p") 所有<p>元素 $("input") 所有input元素 $(".intro&qu ...
- sparkR原理
p.p1 { margin: 0.0px 0.0px 10.0px 0.0px; font: 11.0px "Times New Roman"; min-height: 12.0p ...
- python函数的返回值 讲解
我们一起来聊聊python函数返回值的特殊情况,之前我也碰到过类似方面的问题,到后来查阅了一些资料后,发现原来是这样. 首先,写函数的时候,一定要写函数的文档,这样方便我们识别函数是做什么的.我记得很 ...
- Hbase实例
import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.had ...