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 ...
随机推荐
- boost circularBuffer
1. circular buffer has two fundamental properties: (1): The capacity of the circular buffer is const ...
- jmeter函数 助手
Jmeter(一)——函数助手 __char:把一组数字转化成Unicode __counter:计数器,支持多线程(可以理解为多用户) 线程组设置为2个用户,循环两次${__counter(true ...
- dotnet 跨平台编译发布
dotnet publish 命令,bash脚本如下(Windows安装git即可建议sh关联) publish.sh #!/usr/bin/env bash # one line command: ...
- 富文本框编辑器实现:a、支持图片复制粘贴;b、支持word复制粘贴图文。
Chrome+IE默认支持粘贴剪切板中的图片,但是我要发布的文章存在word里面,图片多达数十张,我总不能一张一张复制吧?Chrome高版本提供了可以将单张图片转换在BASE64字符串的功能.但是无法 ...
- AT2000 Leftmost Ball(计数dp+组合数学)
传送门 解题思路 设\(f[i][j]\)表示填了\(i\)个白色,\(j\)种彩色的方案数,那么显然\(j<=i\).考虑这个的转移,首先可以填一个白色,就是\(f[i][j]=f[i-1][ ...
- angularjs的select使用及默认选中
1 ng-model="standardCourse.showHours"代替name 2 ng-selected = "1"代替selected=" ...
- Spring JDK动态代理
1. 创建项目在 MyEclipse 中创建一个名称为 springDemo03 的 Web 项目,将 Spring 支持和依赖的 JAR 包复制到 Web 项目的 WEB-INF/lib 目录中,并 ...
- Dealing with exceptions thrown in Application_Start()
https://blog.richardszalay.com/2007/03/08/dealing-with-exceptions-thrown-in-application_start/ One a ...
- 浅谈 STM32 硬件I2C的使用 (中断方式 无DMA 无最高优先级)(转)
引子 STM32的硬件I2C很多人都对它望而却步.因为很多电工都说,STM32 硬件 I2C有BUG.不稳定.死机等等……最后都使用GPIO模拟I2C. 的确,模拟I2C好用.但是在我看来在一个72M ...
- C#-Newtonsoft.Json生成复杂JSON
官方文档:https://www.newtonsoft.com/json/help/html/SerializeObject.htm 一种方式就可以生成所有的 JSON Collection -> ...