要你求两个非常大的数字的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. 升级Xcode或 MacOS编译iOS出现resource fork, Finder information, or similar detritus not allowed

    很久没有在网上留下足迹了,冒个泡吧 最近升级了Xcode,编译之前的一个项目是出现问题,问题结尾如下: resource fork, Finder information, or similar de ...

  2. iOS CoreData 开发

    新年新气象,曾经的妹子结婚了,而光棍的我决定书写博客~ 废话结束. 本人不爱使用第三方的东东,喜欢原汁原味的官方版本,本次带来CoreData数据存储篇~ 创建应用

  3. 删除过期备份报错RMAN-06207 RMAN-06208解决方案

    RMAN备份日志中出现了警告 日志文件目录如下: [root@erpdbs rmanback]# ll total 88 -rw-r--r-- 1 oraprod dba 81011 Sep 7 22 ...

  4. 安卓手机安装charles安全证书

    本次安装使用小米mix2为例. 手机浏览器上安装: 第一种: 1.首先 设置好手机的charles代理   172.16.xxx.xxx   8888 2.要使用 打开非自带浏览器(夸克/QQ/UC手 ...

  5. solaris roles cannot login directly

    oracle@solaris:~$ su - root Password: Oracle Corporation SunOS root@solaris:~# cat /etc/user_attr # ...

  6. 【Android】一个好用的sharedpreferences存储类方法

    其实我也不知道sharedpreferences究竟安全不安全,毕竟是android中最简单的存储机制. 如果你手机root了的话,使用MT管理器到data/data/包名/shared_prefs下 ...

  7. JAVA学习总结-常用数据结构

    java中集合框架其实就是数据结构的实现的封装; 参考资料:任小龙教学视频 1,什么是数据结构? 数据结构是计算机存储,组织数据的方式; 数据结构是指相互之间存在一种或多种特定关系的数据元素的集合; ...

  8. RaspberryPi3安装CentOS7教程

    1.准备 Centos 7 AMR版镜像下载地址: http://mirror.centos.org/altarch/7/isos/armhfp/ 下载得到:CentOS-Userland-7-arm ...

  9. HDU - 3556 - Continued Fraction

    先上题目: Continued Fraction Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Jav ...

  10. GMT,UTC,DST,CST时间详解

    全球24个时区的划分       相较于两地时间表,可以显示世界各时区时间和地名的世界时区表(World Time),就显得精密与复杂多了,通常世界时区表的表盘上会标示着全球24个时区的城市名称,但究 ...