题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=1876

  一道简单的高精度+Stein算法(或者叫辗转相除法)求最大公约数板子题。

  md还要压位。。

  代码:

#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#define ll long long
#define ull unsigned long long
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define lowbit(x) (x& -x)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define eps 1e-18
#define maxn 100010
inline ll read(){ll tmp=; char c=getchar(),f=; for(;c<''||''<c;c=getchar())if(c=='-')f=-; for(;''<=c&&c<='';c=getchar())tmp=(tmp<<)+(tmp<<)+c-''; return tmp*f;}
inline ll power(ll a,ll b){ll ans=; for(;b;b>>=){if(b&)ans=ans*a%mod; a=a*a%mod;} return ans;}
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline void swap(int &a,int &b){int tmp=a; a=b; b=tmp;}
using namespace std;
const int base=;
struct Bignum{
int len;
int num[];
friend bool operator < (Bignum a,Bignum b){
if(a.len!=b.len)return a.len<b.len;
for(int i=a.len;i;i--)
if(a.num[i]!=b.num[i])return a.num[i]<b.num[i];
return ;
}
friend Bignum operator - (Bignum a,Bignum b){
Bignum c;
memset(&c,,sizeof(c));
for(int i=;i<=a.len;i++){
if(a.num[i]<b.num[i])a.num[i]+=base,--a.num[i+];
c.num[i]=a.num[i]-b.num[i];
}
c.len=a.len;
while(c.len>&&!c.num[c.len])--c.len;
return c;
}
friend Bignum operator * (Bignum a,int b){
Bignum c;
memset(&c,,sizeof(c)); ll tmp=;
for(int i=;i<=a.len;i++){
tmp+=a.num[i]*b;
c.num[i]=tmp%base; tmp/=base;
}
c.len=a.len;
for(;tmp;tmp/=base)c.num[++c.len]=tmp%base;
return c;
}
friend Bignum operator / (Bignum a,int b){
Bignum c;
memset(&c,,sizeof(c)); ll tmp=;
for(int i=a.len;i;i--){
tmp=tmp*base+a.num[i];
c.num[i]=tmp/b; tmp%=b;
}
c.len=a.len;
while(c.len>&&!c.num[c.len])--c.len;
return c;
}
void print(Bignum a){
printf("%d",a.num[a.len]);
for(int i=a.len-;i;i--)
printf("%09d",a.num[i]);
printf("\n");
}
}a,b;
char A[],B[];
int n,m;
Bignum gcd(Bignum a,Bignum b)
{
int cnt=;
while(){
if(a<b){
Bignum tmp; tmp=a; a=b; b=tmp;
}
if(b.len==&&b.num[]==)break;
if(a.num[]&){
if(b.num[]&){
Bignum tmp; tmp=b; b=a-b; a=tmp;
}
else b=b/;
}
else{
if(b.num[]&)a=a/;
else ++cnt,a=a/,b=b/;
}
}
Bignum ans=a;
for(int i=;i<=cnt;i+=)ans=ans*(<<);
for(int i=cnt%;i;i--)ans=ans*;
return ans;
}
int main()
{
scanf("%s",A); n=strlen(A);
scanf("%s",B); m=strlen(B);
a.len=b.len=;
for(int i=n-;i>=;i-=){
a.num[++a.len]=;
for(int j=max(i-,);j<=i;j++)
a.num[a.len]=a.num[a.len]*+A[j]-'';
}
for(int i=m-;i>=;i-=){
b.num[++b.len]=;
for(int j=max(i-,);j<=i;j++)
b.num[b.len]=b.num[b.len]*+B[j]-'';
}
Bignum ans=gcd(a,b);
ans.print(ans);
}

bzoj1876

【bzoj1876】[SDOI2009]SuperGCD(高精度)的更多相关文章

  1. [BZOJ1876][SDOI2009]superGCD(高精度)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1876 分析: 以为辗转相减会TLE呢……但是好像没这个数据……就这么水过去了…… 辗转 ...

  2. bzoj1876: [SDOI2009]SuperGCD

    更相减损数. 上手就debug了3个小时,直接给我看哭了. 3个函数都写错了是什么感受? 乘2函数要从前往后乘,这样后面的数乘2进位以后不会干扰前面的数. 除2函数要从后往前除,这样前面的数借来的位不 ...

  3. bzoj千题计划288:bzoj1876: [SDOI2009]SuperGCD

    http://www.lydsy.com/JudgeOnline/problem.php?id=1876 高精压位GCD 对于  GCD(a, b)  a>b 若 a 为奇数,b 为偶数,GCD ...

  4. BZOJ1876 [SDOI2009]SuperGCD 【高精 + GCD优化】

    题目 Sheng bill有着惊人的心算能力,甚至能用大脑计算出两个巨大的数的GCD(最大公约 数)!因此他经常和别人比 赛计算GCD.有一天Sheng bill很嚣张地找到了你,并要求和你比 赛,但 ...

  5. 【BZOJ1876】[SDOI2009]SuperGCD(数论,高精度)

    [BZOJ1876][SDOI2009]SuperGCD(数论,高精度) 题面 BZOJ 洛谷 题解 那些说数论只会\(gcd\)的人呢?我现在连\(gcd\)都不会,谁来教教我啊? 显然\(gcd\ ...

  6. bzoj 1876 [SDOI2009]SuperGCD(高精度+更相减损)

    1876: [SDOI2009]SuperGCD Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 2384  Solved: 806[Submit][Sta ...

  7. BZOJ 1876: [SDOI2009]SuperGCD( 更相减损 + 高精度 )

    更相减损,要用高精度.... --------------------------------------------------------------- #include<cstdio> ...

  8. BZOJ 1876: [SDOI2009]SuperGCD

    1876: [SDOI2009]SuperGCD Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 3060  Solved: 1036[Submit][St ...

  9. BZOJ1876:[SDOI2009]SuperGCD——C++高精度良心题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1876 Description Sheng bill有着惊人的心算能力,甚至能用大脑计算出两个巨大的数 ...

随机推荐

  1. Python 对Twitter tweet的元素 (Word, Screen Name, Hash Tag)的频率分析

    CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-7-2 @author: guaguastd @name: tw ...

  2. unintest基础1

    import unittest class Testfunc(unittest.TestCase): def testfunc(self): print('testfunc1') def testfu ...

  3. fork小续

    pid_t pid = fork(); 1.根据fork的返回值区分父子进程: fork 函数返回两次, >0 表示父进程,返回值为子进程ID; =0 表示子进程; <0 出错. 可用代码 ...

  4. Sleep Buddies

    Sleep Buddies time limit per test 2.0 s memory limit per test 256 MB input standard input output sta ...

  5. 05、(通过nat123软件) 实现用自己电脑搭建一个网站

    (通过nat123软件) 实现用自己电脑搭建一个网站 准备: Tomcat:这个是web容器,其实有了这个就已经让电脑成为服务器了,在自己电脑上可以通过 localhost:8080/xxx 来访问自 ...

  6. linux中常见的文件操作命令

    由于经常在linux发布工程进行测试,所以要用到linux一些文件操作命令,再此进行总结,以便以后忘记的时候查看. 改变目录:cd 回到家目录 cd或者cd~ 查看当前目录:pwd 查看目录下的文件的 ...

  7. 对宽度的控制原则 git commit -a -m "M 1、完成less计算得出图片的均分布局;";git push origin master:master

    <script> import wepy from 'wepy' import api from '../api/api' export default class recharge ex ...

  8. Constructor Acquires, Destructor Releases Resource Acquisition Is Initialization

    w https://zh.wikipedia.org/wiki/RAII RAII要求,资源的有效期与持有资源的对象的生命期严格绑定,即由对象的构造函数完成资源的分配(获取),同时由析构函数完成资源的 ...

  9. fatal error C1083: 无法打开包括文件:“stdint.h”: No such file or directory

    stdint.h是c99标准的头文件,vc不支持,所以肯定会提示“No such file or directory”的. stdint.h是C99的标准,主要用于统一跨平台数据定义. MSVC中不带 ...

  10. 【转】图解MySql命令行创建存储过程

    一 操作实例 首先登录mysql: 使用source命令,从命令行执行sql脚本,创建表: 创建第一个存储过程: 事先用DELIMITER关键字申明当前段分隔符,这样MySQL才会将";&q ...