题目传送门: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. laravel类自动注释

    github地址:https://github.com/barryvdh/laravel-ide-helper php artisan ide-helper:models --dir="pa ...

  2. Android开发:《Gradle Recipes for Android》阅读笔记(翻译)3.4——Flavor Dimensions

    问题: 一个product flavor不够,你需要另一个标准去区分不同版本的app 解决方案: 在product flavor中增加flavorDimensions 讨论: 在3.2章展示了一个有三 ...

  3. 在eclipse里面使用git仓库,并且使用maven加载项目

    前提:安装完成git和maven(maven需要在eclipse中配置完成) 1.点击Windows->show view 选择Git Repositories(git仓库) 2.点击2号图标, ...

  4. poj1026(置换找循环节)

    找到循环节,然后对应的变换 Cipher Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20377   Accepted:  ...

  5. Spec Explorer 工具学习

    基础概念:http://blogs.msdn.com/b/sechina/archive/2009/12/28/test.aspx 在线教程:http://blogs.msdn.com/b/sechi ...

  6. &#x编码转换成汉字

    import java.io.UnsupportedEncodingException; public class UnicodeDecoder { public static String esca ...

  7. 20160916-3:mysql主从复制

    一.什么是主从复制 将一个数据库节点的数据拷贝到一个或多个数据库节点(主节点—>从节点) 二.主从复制的原理 [简述]:将主节点上的变更操作存储到binlog,从节点建立了到主节点的复制关系后, ...

  8. Redis 单机版本安装

    亲装! 1.linux 系统镜像 redis 版本  使用redis-3.2.8.tar.gz(截止2017年4月的最新稳定版) 在安装之前先安装下redis 需要的环境 wget http://do ...

  9. 新提交审核app保留检查更新入口将被拒绝

    3月起要求关闭所有App内的检查更新功能,苹果App Store将向用户自动提示更新,新提交审核版本如果保留检查更新入口审核时将被拒绝,请各产品团队重点关注. 10.6 - Apple and our ...

  10. MVC4学习笔记之--身份认证过滤器

    过滤器作为MVC模式中面向切面编程应用很广泛,例如身份验证,日志,异常,行为截取等.博客园里面的大神对应过滤器的介绍以及很多,MVC4中不同的过滤器也介绍得很清楚.FlyDragon  辉太 禁止吸烟 ...