bzoj 1876
传送门 http://www.lydsy.com/JudgeOnline/problem.php?id=1876
二进制gcd 学到了(' ' ) 高精还得压位,最开始没写压位,然后调了1h后又重写了一遍(' ' ) 怨念深重
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = 10010;
const int inf = 1000000000; struct bgint {
int n, a[maxn];
bgint() {
n = 0; memset(a, 0, sizeof(a));
}
bgint operator - (const bgint &x) const {
bgint ret; ret.n = max(x. n, this-> n);
for(int i = 1; i <= ret. n; ++ i) {
ret. a[i] += this-> a[i] - x. a[i];
if(ret. a[i] < 0) ret. a[i] += inf, ret. a[i + 1] --;
}
while(ret. a[ret. n] == 0 && ret. n != 1) ret. n --;
return ret;
}
}; bgint mov(const bgint &x) {
bgint ret; ret. n = x. n;
for(int i = 1; i <= ret. n; ++ i) {
ret. a[i] += x. a[i] * 2;
ret. a[i + 1] += ret. a[i] / inf;
ret. a[i] %= inf;
}
if(ret. a[ret. n + 1]) ret. n ++;
return ret;
} bgint div(const bgint &x) {
bgint ret;
if(x. a[x.n] < 2) {
ret. n = x. n - 1;
for(int i = 1; i <= ret. n; ++ i) ret. a[i] = x. a[i];
ret. a[ret. n] += x. a[x.n] * inf;
for(int i = ret. n; i >= 1; -- i) ret. a[i - 1] += ret. a[i] % 2 * inf, ret. a[i] /= 2;
ret. a[0] = 0;
}
else {
ret. n = x.n;
for(int i = 1; i <= ret. n; ++ i) ret. a[i] = x. a[i];
for(int i = ret. n; i >= 1; -- i) ret. a[i - 1] += ret. a[i] % 2 * inf, ret. a[i] /= 2;
ret. a[0] = 0;
}
return ret;
} bool even (const bgint &x) {
return (x. a[1] & 1);
} bool is (const bgint &x, const bgint &t) {
if(x. n != t. n) return 0;
for(int i = 1; i <= x. n; ++ i) if(x. a[i] != t. a[i]) return 0;
return 1;
} bool com(const bgint &x, const bgint &t) {
if(x. n > t. n) return 0;
else if(x. n < t. n) return 1;
for(int i = x. n; i >= 1; -- i) {
if(x. a[i] > t. a[i]) return 0;
else if(x. a[i] < t.a[i]) return 1;
}
return 0;
} void print(const bgint &x) {
for(int i = x. n; i >= 1; -- i) {
if(i == x.n ) printf("%d", x. a[i]);
else printf("%09d", x. a[i]);
}
} bgint gcd(bgint a, bgint b) {
bgint ret; ret. a[1] = 1, ret. n = 1;
int cnt = 0;
while(!is(a, b)) {
if(even(a)) {
if(even(b)) {
if(!com(a, b)) a = div((a - b));
else b = div((b - a));
}
else b = div(b);
}
else {
if(even(b)) a = div(a);
else b = div(b), a = div(a), cnt ++;
}
// ++ x; print(a); cout << " "; print(b); cout << endl;
}
for(int i = 1; i <= cnt; ++ i) a = mov(a);
return a;
} char s[maxn]; bgint read() {
scanf("%s", s + 1);
bgint ret; ret. n = (int)strlen(s + 1); ret. n = ret. n % 9 == 0 ? ret. n / 9 : ret. n / 9 + 1;
int sl = (int)strlen(s + 1);
for(int i = 1; i <= ret. n; ++ i) {
int t = 1;
for(int j = (i - 1) * 9 + 1; j <= min(i * 9, sl); ++ j)
ret. a[i] += (int)(s[sl - j + 1] - '0') * t, t *= 10;
}
return ret;
} void sov() {
bgint x = read(), t = read();
print(gcd(x, t));
} int main() {
//freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
sov();
return 0;
}
bzoj 1876的更多相关文章
- BZOJ 1876: [SDOI2009]SuperGCD
1876: [SDOI2009]SuperGCD Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 3060 Solved: 1036[Submit][St ...
- bzoj 1876 [SDOI2009]SuperGCD(高精度+更相减损)
1876: [SDOI2009]SuperGCD Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 2384 Solved: 806[Submit][Sta ...
- BZOJ 1876: [SDOI2009]SuperGCD( 更相减损 + 高精度 )
更相减损,要用高精度.... --------------------------------------------------------------- #include<cstdio> ...
- BZOJ 1876 SuperGCD
Description Sheng bill有着惊人的心算能力,甚至能用大脑计算出两个巨大的数的GCD(最大公约数)!因此他经常和别人比赛计算GCD.有一天Sheng bill很嚣张地找到了你,并要求 ...
- 「BZOJ 1876」「SDOI 2009」SuperGCD「数论」
题意 求\(\gcd(a, b)\),其中\(a,b\leq10^{10000}\) 题解 使用\(\text{Stein}\)算法,其原理是不断筛除因子\(2\)然后使用更相减损法 如果不筛\(2\ ...
- bzoj 1876 高精
首先我们知道,对于两个数a,b,他们的gcd情况有如下形式的讨论 当a为奇数,b为偶数的时候gcd(a,b)=gcd(a div 2,b) 当b为奇数,a为偶数的时候gcd(a,b)=gcd(a,b ...
- BZOJ 1876 [SDOI2009] SuperGcd | PY好题
题面就是让你求两个超级大整数,求GCD 题解: 题目本意应该是出题人想考考高精度取膜 但是可以通过一种神奇的Stein算法来做 由J. Stein 1961年提出的Stein算法很好的解决了欧几里德算 ...
- [SDOI2009][BZOJ 1876]SuperGCD
Description Sheng bill有着惊人的心算能力,甚至能用大脑计算出两个巨大的数的GCD(最大公约 数)!因此他经常和别人比 赛计算GCD.有一天Sheng bill很嚣张地找到了你,并 ...
- 【BZOJ】【1876】【SDOI2009】SuperGCD
高精度+GCD 唔……高精gcd其实可以这么算: \[ GCD(a,b)= \begin{cases} a & b=0 \\ 2*GCD(\frac{a}{2},\frac{b}{2}) &a ...
随机推荐
- windows之cmd常用命令
一.简单介绍 CMD全称command,即命令提示符,是内置在windows图形操作系统内的磁盘操作系统,通过CMD可以方便用户查询比较复杂的信息或快速查找实现某些功能等,比如说打开文件.系统设置等操 ...
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 GSM Base Station Identification (点在多边形内模板)
In the Personal Communication Service systems such as GSM (Global System for Mobile Communications), ...
- Agri-Net —poj1258
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44670 Accepted: 18268 Descri ...
- python中递归函数
python中的 递归函数,是指的是函数在函数内部调用自己的函数 需要满足两个条件,一,需要有一个明确的终止条件 二,需要函数自己在内部调用自己
- JSP 获取访问者真正的IP地址
request.getRemoteAddr(),这种方法在大部分情况下都是有效的,但是在通过了Apache,Squid等反向代理软件就不能获取到客户端的真实IP地址了, 如果使用了反向代理软件,用re ...
- Hibernate 异常org.hibernate.LazyInitializationException: could not ini...
错误页面提示 could not initialize proxy - no Session 控制台 org.hibernate.LazyInitializationException: could ...
- Font and PDF
1. 独立存在的Font文件 有三类: Type 1 Font TrueType Font OpenType Font Type 1 是由Adobe开发的,它是基于PostScript的Font,它通 ...
- T1387:搭配购买(buy)
[题目描述] Joe觉得云朵很美,决定去山上的商店买一些云朵.商店里有n朵云,云朵被编号为1,2,…...,n,并且每朵云都有一个价值.但是商店老板跟他说,一些云朵要搭配来买才好,所以买一朵云则与这朵 ...
- Cocos2d Box2D之静态刚体
| 版权声明:本文为博主原创文章,未经博主允许不得转载. b2_staticBody 在模拟环境下静态物体是不会移动的,就好像有无限大的质量.在Box2D的内部会将质量至反,存储为零.静态物体也可 ...
- bash 中的特殊变量
bash 中的特殊变量 变量 说明 ------------------------------------ $0 当前脚本的文件名 $n 第n个位置参数 $* 传递给脚本或函数的所有参数,$*会将这 ...