要你求两个非常大的数字的GCD。

  不要想复杂,用高精度整更相减损术即可。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; struct BigInt
{
static const int BASE = 10000, CARRY = 4, MAX_N = 10000;
int A[MAX_N], Len; void Clear()
{
memset(A, 0, sizeof(A));
Len = 0;
} void Read(char *s)
{
int len = strlen(s);
Clear();
int cur = 0, pos = 0, pow = 1;
for (int i = len - 1; i >= 0; i--)
{
cur += (s[i] - '0') * pow;
pow *= 10;
if (++pos == CARRY)
{
A[Len++] = cur;
cur = pos = 0;
pow = 1;
}
}
if (!pos)
Len--;
else
A[Len] = cur;
} void Print()
{
printf("%d", A[Len]);
for (int i = Len - 1; i >= 0; i--)
printf("%0*d", CARRY, A[i]);
printf("\n");
} void operator -= (const BigInt& a)
{
for (int i = 0; i <= Len; i++)
{
A[i] -= a.A[i];
if (A[i] < 0)
{
A[i + 1]--;
A[i] += BASE;
}
}
while (A[Len] == 0 && Len > 0)
Len--;
} bool operator < (const BigInt& a) const
{
if (Len != a.Len)
return Len < a.Len;
for (int i = Len; i >= 0; i--)
if (A[i] != a.A[i])
return A[i] < a.A[i];
return false;
} bool operator != (const BigInt& a) const
{
if (Len != a.Len)
return true;
for (int i = Len; i >= 0; i--)
if (A[i] != a.A[i])
return true;
return false;
}
}; int main()
{
BigInt *a = new BigInt, *b = new BigInt;
static char s[BigInt::BASE * BigInt::CARRY];
scanf("%s", s);
a->Read(s);
scanf("%s", s);
b->Read(s);
while (*a != *b)
{
if (*a < *b)
swap(a, b);
*a -= *b;
}
a->Print();
return 0;
}

  

luogu2152 [SDOI2009]SuperGCD的更多相关文章

  1. BZOJ 1876: [SDOI2009]SuperGCD

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

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

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

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

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

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

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

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

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

  6. bzoj1876: [SDOI2009]SuperGCD

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

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

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

  8. [SDOI2009]SuperGCD

    题目链接 这题.高精度.恶心.难受. 那么高精度的gcd怎么做呢? 若a=b gcd(a,b)=a ①a偶b偶 gcd(a,b)=2*gcd(a/2,b/2) ②a偶b奇 gcd(a,b)=gcd(a ...

  9. P2152 [SDOI2009]SuperGCD 未完成

    辗转相减求a,b的gcd其实可以优化的: 1.若a为偶数,b为奇数:gcd(a,b)=gcd(a/2,b) 2.若a为奇数,b为偶数:gcd(a,b)=gcd(a,b/2) 3.若a,b都是偶数:gc ...

随机推荐

  1. cplusplus系列>utility>pair

    http://www.cplusplus.com/reference/utility/pair/ 用于存储一对异构对象 // Compile: g++ -std=c++11 pair.cpp #inc ...

  2. Object未定义

    js加载时会在jquery中报Object未定义的错误? 原因: 页面和iframe页面中都引入了jquery 或者 是页面中包含iframe,并且在iframe没有完成加载前操作了iframe中的j ...

  3. C# Task多线程

    来自Eleven老师示例 private void btnTask_Click(object sender, EventArgs e) { Console.WriteLine(); Console.W ...

  4. Zabbix 监控磁盘IO

    Zabbix 监控磁盘IO 1.数据获取脚本 #!/bin/bash # resource: http://www.muck.net/19/getting-hard-disk-performance- ...

  5. 浅谈animation里的forwards

    forwards可译为向前走, animation-fill-mode(动画填充模式),定义动画播放时间之外的状态 顾名思义,就是在动画播放完了之后给它一个状态 animation-fill-mode ...

  6. @Order

    1.Spring 4.2 利用@Order控制配置类的加载顺序, 2.Spring在加载Bean的时候,有用到order注解. 3.通过@Order指定执行顺序,值越小,越先执行 4.@Order注解 ...

  7. stress工具使用指南和结果分析

    stress介绍 #stress `stress' imposes certain types of compute stress on your system Usage: stress [OPTI ...

  8. Spring框架学习之SpringAOP(二)

    AOP概念 AOP(Aspect-Oriented Programming,面向切面编程),AOP是OOP(面向对象编程)的补充和完善 AOP的核心思想就是“将应用程序中的商业逻辑同对其提供支持的通用 ...

  9. C#学习笔记_07_数组

    07_数组 数组的声明与实例化 名词解释 数组:数组是一个容器,用来存储一系列相兼容的数据类型的变量: 实例化:声明一个数组,并且赋初始值: 数组长度:就是数组的容量,表示这个数组可以存储多少个数据: ...

  10. CTF实战(隐写术):欢迎来到地狱

    前言:请登录实验吧开启刷题模式,拿到的flag返回该网站验证正确性. 下载“欢迎来到地狱”CTF题目(总共三个文件