题意简述

求两个整数a,b的最大公约数0 < a , b ≤ 10 ^ 10000。

题解思路

如果 a % 2 == 0 && b % 2 == 0 gcd(a,b) = gcd(a / 2, b / 2) * 2

如果 a % 2 == 0 && b % 2 != 0 gcd(a,b) = gcd(a / 2, b);

如果 a % 2 != 0 && b % 2 == 0 gcd(a,b) = gcd(a, b / 2);

如果 a % 2 != 0 && b % 2 != 0 gcd(a,b) = gcd(a - b, b);

代码

#include <cstring>
#include <iostream>
using namespace std;
struct Number
{
int a[100000];
int c;
Number& operator=(const Number& rhs)
{
c = rhs.c;
for (register int i = 1; i <= c; ++i)
a[i] = rhs.a[i];
return *this;
}
}n1, n2, ans, xx;
char st[100000];
bool b1, b2;
void print(const Number &x)
{
if (!x.c) {cout << 0 << endl; return;}
for (register int i = x.c + 1; --i; ) cout << x.a[i];
cout << endl;
}
bool compare(const Number &x, const Number &y)
{
if (x.c != y.c) return x.c < y.c;
for (register int i = x.c + 1; --i; )
if (x.a[i] != y.a[i])
return x.a[i] < y.a[i];
return 0;
}
void _swap(Number &x, Number &y)
{
Number t;
t = x; x = y; y = t;
}
void div(Number &x)
{
if (x.a[x.c] == 1) x.a[x.c] = 0, x.a[--x.c] += 10;
for (register int i = x.c; i; --i)
if (x.a[i] & 1)
{
x.a[i] /= 2;
x.a[i - 1] += 10;
}
else x.a[i] /= 2;
}
void mul(Number &x, const Number &y)
{
Number c;
memset(c.a, 0, sizeof c.a);
c.c = x.c + y.c - 1;
for (register int i = 1; i <= x.c; ++i)
for (register int j = 1; j <= y.c; ++j)
{
c.a[i + j - 1] += x.a[i] * y.a[j];
c.a[i + j] += c.a[i + j - 1] / 10;
c.a[i + j - 1] %= 10;
}
while (c.a[c.c + 1]) ++c.c;
_swap(c, x);
}
void sub(Number &x, const Number &y)
{
for (register int i = 1; i <= y.c; ++i)
{
x.a[i] -= y.a[i];
if (x.a[i] < 0)
{
x.a[i] += 10;
x.a[i + 1] -= 1;
}
}
int xx = y.c + 1;
while (x.a[xx] < 0) x.a[xx] += 10, x.a[++xx] -= 1;
while (!x.a[x.c] && x.c > 0) --x.c;
}
int main()
{
ans.a[++ans.c] = 1;
xx.a[++xx.c] = 2;
ios::sync_with_stdio(0);
cin >> st;
n1.c = strlen(st);
for (register int i = n1.c; i; --i)
n1.a[i] = st[n1.c - i] - '0';
cin >> st;
n2.c = strlen(st);
for (register int i = n2.c; i; --i)
n2.a[i] = st[n2.c - i] - '0';
if (compare(n1, n2)) _swap(n1, n2);
while (n2.c)
{
while (!(n1.a[1] & 1) && !(n2.a[1] & 1))
{
mul(ans, xx);
div(n1);
div(n2);
}
if (!(n1.a[1] & 1)) div(n1);
else if (!(n2.a[1] & 1)) div(n2);
else sub(n1, n2);
if (compare(n1, n2)) _swap(n1, n2);
}
mul(ans, n1);
print(ans);
}

洛谷 P2152 [SDOI2009]SuperGCD的更多相关文章

  1. 洛谷 P2152 [SDOI2009]SuperGCD (高精度)

    这道题直接写了我两个多小时-- 主要是写高精度的时候还存在着一些小毛病,调了很久 在输入这一块卡了很久. 然后注意这里用while的形式写,不然会炸 最后即使我已经是用的万进制了,但是交上去还是有两个 ...

  2. 洛谷 2152 [SDOI2009]SuperGCD

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

  3. 洛谷P1972 [SDOI2009]HH的项链 题解

    [SDOI2009]HH的项链 题目背景 无 题目描述 HH 有一串由各种漂亮的贝壳组成的项链.HH 相信不同的贝壳会带来好运,所以每次散步完后,他都会随意取出一段贝壳,思考它们所表达的含义.HH 不 ...

  4. BZOJ1880或洛谷2149 [SDOI2009]Elaxia的路线

    BZOJ原题链接 洛谷原题链接 显然最长公共路径是最短路上的一条链. 我们可以把最短路经过的边看成有向边,那么组成的图就是一张\(DAG\),这样题目要求的即是两张\(DAG\)重合部分中的最长链. ...

  5. 洛谷P2148 [SDOI2009]E&D(博弈论)

    洛谷题目传送门 先安利蒟蒻仍在施工的博弈论总结 首先根据题目,石子被两两分组了,于是根据SG定理,我们只要求出每一组的SG值再全部异或起来就好啦. 把每一对数看成一个ICG,首先,我们尝试构造游戏的状 ...

  6. BZOJ1227或洛谷2154 [SDOI2009]虔诚的墓主人

    BZOJ原题链接 洛谷原题链接 又是扫描线,题解可看大佬的博客(太懒了不想打) #include<cstdio> #include<algorithm> using names ...

  7. BZOJ1228或洛谷2148 [SDOI2009]E&D

    BZOJ原题链接 洛谷原题链接 完全不会呀.. 写了这题才知道\(SG\)函数原来也能打表找规律... 题解请看大佬的博客 #include<cstdio> using namespace ...

  8. BZOJ1226或洛谷2157 [SDOI2009]学校食堂

    BZOJ原题链接 洛谷原题链接 注意到\(B[i]\)很小,考虑状压\(DP\). 设\(f[i][j][k]\)表示前\(i - 1\)个人已经拿到菜,第\(i\)个人及其后面\(7\)个人是否拿到 ...

  9. [洛谷P1972][SDOI2009]HH的项链

    题目大意:给你一串数字,多次询问区间内数字的种类数 题解:莫队 卡点:洛谷数据加强,开了个$O(2)$ C++ Code: #include <cstdio> #include <a ...

随机推荐

  1. 腾讯架构师分享的Java程序员需要突破的技术要点

    一.源码分析 源码分析是一种临界知识,掌握了这种临界知识,能不变应万变,源码分析对于很多人来说很枯燥,生涩难懂. 源码阅读,我觉得最核心有三点:技术基础+强烈的求知欲+耐心. 我认为是阅读源码的最核心 ...

  2. 《ElasticSearch6.x实战教程》之准备工作、基本术语

    第一章-准备工作 工欲善其事必先利其器 ElasticSearch安装 ElasticSearch6.3.2下载地址(Linux.mac OS.Windows通用,下载zip包即可):https:// ...

  3. Java编程思想:为什么要使用内部类

    public class Test { public static void main(String[] args) { Callbacks.test(); } } /* 为什么需要内部类: 1.可以 ...

  4. Java中的Lambda表达式简介及应用

    在接触Lambda表达式.了解其作用之前,首先来看一下,不用Lambda的时候我们是怎么来做事情的. 我们的需求是,创建一个动物(Animal)的列表,里面有动物的物种名,以及这种动物是否会跳,是否会 ...

  5. Docker实现GPA+Exporter监控告警系统

    Docker实现GPA+Exporter监控告警系统 1.搭建grafana,prometheus,blackbox_exporter环境 # docker run -d -p 9090:9090 - ...

  6. 使用flink Table &Sql api来构建批量和流式应用(3)Flink Sql 使用

    从flink的官方文档,我们知道flink的编程模型分为四层,sql层是最高层的api,Table api是中间层,DataStream/DataSet Api 是核心,stateful Stream ...

  7. Netty-Pipeline深度解析

    首先我们知道,在NIO网络编程模型中,IO操作直接和channel相关,比如客户端的请求连接,或者向服务端发送数据, 服务端都要从客户端的channel获取这个数据 那么channelPipeline ...

  8. NOIP2018&2013提高组T1暨洛谷P5019 铺设道路

    题目链接:https://www.luogu.org/problemnew/show/P5019 花絮:普及蒟蒻终于A了一道提高的题目?emm,写一篇题解纪念一下吧.求过! 分析: 这道题我们可以采用 ...

  9. 鸟哥的Linux私房菜笔记第六章(二)

    文件内容查询 直接查询文件内容 查阅一个文件的内容可以使用指令cat/tac/nl. # [cat|tac|nl] 文件 区别: 1.cat是直接把文件内容输出到屏幕上,并且从第一行开始输出到末行 2 ...

  10. 小白之入口即化——十分钟看懂while循环,字符串格式化,运算符

    while循环 while循环-死循环 while空格+条件+冒号 缩进+循环体 3.打断死循环 break--终止当前循环 while True: print(123) print(234) bre ...