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 ...
随机推荐
- maven之可执行jar包
在使idea创建springboot项目时,pom.xml文件中自动会添加下面这个插件. <build> <plugins> <plugin> <groupI ...
- nofollow标签的作用 nofollow标签添加方法
这篇文章主要介绍了nofollow标签的作用 nofollow标签添加方法,需要的朋友可以参考下 nofollow标签的作用 nofollow标签添加方法 模拟搜狗蜘蛛 nofollow标签 ...
- thinkphp 配置格式
ThinkPHP支持多种格式的配置格式,但最终都是解析为PHP数组的方式. PHP数组定义 返回PHP数组的方式是默认的配置定义格式,例如: //项目配置文件 return [ // 默认模块名 'd ...
- PHP curl_multi_add_handle函数
curl_multi_add_handle — 向curl批处理会话中添加单独的curl句柄 说明 int curl_multi_add_handle ( resource $mh , resourc ...
- Appium运行报错:No Chromedriver found that can automate Chrome '39.0.0'
运行appium切换webview时候遇到报错:’No Chromedriver found that can automate Chrome 'xx.xx.xx' 此报错是因为Appium在运行过程 ...
- vue和php-前后台交互
vue和php-前后台交互 前端主要代码: <template> <div class="main-member-info"> <form @subm ...
- 渗透测试工具sqlmap基础教程
转载请注明出处:http://blog.csdn.net/zgyulongfei/article/details/41017493 作者:羽龍飛 本文仅献给想学习渗透测试的sqlmap小白,大牛请绕过 ...
- 剑指offer---4、序列化二叉树
剑指offer---4.序列化二叉树 一.总结 一句话总结: 1. 对于序列化:使用前序遍历,递归的将二叉树的值转化为字符,并且在每次二叉树的结点不为空时,在转化val所得的字符之后添加一个' , ' ...
- Hibernate中常用HQL
HQL是Hibernate自带的查询语言 HQL是一种面向对象的查询语言.SQL的操作对象是数据表和列等数据对象,而HQL的操作对象是类.实例.属性等. HQL的语法很像SQL的语法 以下举例均以学生 ...
- 如何在列表,字典,集合中,根据条件筛选数据 -- Python数据结构与算法相关问题与解决技巧
实际案例: 1.过滤掉列表 [3,9,-1,10,20,-2..]的负数 2.筛出字典{'LiLei':79,'Jim':88,'Lucy':92...}中值高于90的项 3.筛出集合 {77,89, ...