luogu2152 [SDOI2009]SuperGCD
要你求两个非常大的数字的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的更多相关文章
- 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> ...
- 【BZOJ1876】[SDOI2009]SuperGCD(数论,高精度)
[BZOJ1876][SDOI2009]SuperGCD(数论,高精度) 题面 BZOJ 洛谷 题解 那些说数论只会\(gcd\)的人呢?我现在连\(gcd\)都不会,谁来教教我啊? 显然\(gcd\ ...
- [BZOJ1876][SDOI2009]superGCD(高精度)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1876 分析: 以为辗转相减会TLE呢……但是好像没这个数据……就这么水过去了…… 辗转 ...
- bzoj1876: [SDOI2009]SuperGCD
更相减损数. 上手就debug了3个小时,直接给我看哭了. 3个函数都写错了是什么感受? 乘2函数要从前往后乘,这样后面的数乘2进位以后不会干扰前面的数. 除2函数要从后往前除,这样前面的数借来的位不 ...
- bzoj千题计划288:bzoj1876: [SDOI2009]SuperGCD
http://www.lydsy.com/JudgeOnline/problem.php?id=1876 高精压位GCD 对于 GCD(a, b) a>b 若 a 为奇数,b 为偶数,GCD ...
- [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 ...
- 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 ...
随机推荐
- 使用doxmate生成文档
主页:http://html5ify.com/doxmate/ 在windows下面使用doxmate 1. 下载node.js(msi)并安装 http://www.nodejs.org/downl ...
- dnn 添加图片
public string fileUpload() { if (fuPhoto.PostedFile != null && fuPhoto.P ...
- codeforces_738C_二分
C. Road to Cinema time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Resetting the SMC & PRAM
Resetting the SMC & PRAM on portables with a battery you should not remove on your own 1. Shut d ...
- Fiddler构造请求
Fiddler工具是一个http协议调试代理工具,它可以帮助程序员测试或调试程序,辅助web开发. Fiddler工具可以发送向服务端发送特定的HTTP请求以及接受服务器回应的请求和数据,是web调试 ...
- hdu 2084 数塔(简单dp)
题目 简单dp //简单的dp #include<stdio.h> #include<string.h> #include<algorithm> using nam ...
- Oracle存储过程及函数的练习题
--存储过程.函数练习题 --(1)创建一个存储过程,以员工号为参数,输出该员工的工资create or replace procedure p_sxt1(v_empno in emp.empno%t ...
- 升级 Linux 内核版本(编译源代码)
升级内核版本(自己编译源码) 从 linux 官网 https://www.kernel.org/ 下载内核源码 解压 tar -xvf linux-4.16.8.tar.xz cd linux-4. ...
- ansible - 基本用法
目录 ansible - 01 一. 安装与使用 ansible命令格式 查看ansible生成的配置文件 ssh认证方式 ansible的第一个命令 弱口令校验 host-pattern的格式 模块 ...
- 洛谷 2387 NOI2014魔法森林 LCT
[题解] 我们先把边按照$a$值从小到大排序,并按照这个顺序加边. 如果当前要加入的边连接的两点$u$与$v$已经是连通的,那么直接加入这条边就会出现环.这时我们需要删除这个环中$b$值最大的边.因此 ...