BZOJ1876 [SDOI2009]SuperGCD 【高精 + GCD优化】
题目
Sheng bill有着惊人的心算能力,甚至能用大脑计算出两个巨大的数的GCD(最大公约 数)!因此他经常和别人比
赛计算GCD。有一天Sheng bill很嚣张地找到了你,并要求和你比 赛,但是输给Sheng bill岂不是很丢脸!所以你
决定写一个程序来教训他。
输入格式
共两行: 第一行:一个数A。 第二行:一个数B。
0 < A , B ≤ 10 ^ 10000。
输出格式
一行,表示A和B的最大公约数。
输入样例
12
54
输出样例
6
题解
时隔大半年,我回来A这道题啦【当初写的太BUG了】
求GCD,很容一想到辗转相除,而高精不好操作取模,这就用到了辗转相除法的本质:更相减损法
GCD(a,b) = GCD(a,a-b) 【a >b】
然而这样会T,所以我们还要优化:
GCD(a,b) = 2*GCD(a/2,b/2) 【2|a且2|b】
GCD(a,b) = GCD(a/2,b) 【2|a】
GCD(a,b) = GCD(a,b/2) 【2|b】
GCD(a,b) = GCD(a,a-b) 【a >b】
加上个压位高精【高精减法,高精除低精,高精乘低精,高精比较】
就可以A了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#define LL long long int
#define REP(i,n) for (int i = 1; i <= (n); i++)
using namespace std;
const int maxn = 10005,B = 4,Base = 10000,maxm = 100005,INF = 1000000000;
struct NUM{
int s[maxn],len;
NUM() {memset(s,0,sizeof(s)); len = 0;}
};
istream& operator >>(istream& in,NUM& a){
string s;
in>>s;
int temp = 0,t = 1;
for (int i = s.length() - 1; i >= 0; i--){
temp = temp + t * (s[i] - '0');
if (t * 10 == Base) a.s[++a.len] = temp,temp = 0,t = 1;
else t *= 10;
}
if (temp) a.s[++a.len] = temp;
return in;
}
ostream& operator <<(ostream& out,const NUM& a){
if (!a.len) out<<0;
else {
printf("%d",a.s[a.len]);
for (int i = a.len - 1; i > 0; i--) printf("%04d",a.s[i]);
}
return out;
}
bool check(const NUM& a){return !(a.s[1] & 1);}
bool equal(const NUM& a,const NUM& b){
if (a.len != b.len) return false;
REP(i,a.len) if (a.s[i] != b.s[i]) return false;
return true;
}
bool operator <(const NUM& a,const NUM& b){
if (a.len < b.len) return true;
if (a.len > b.len) return false;
for (int i = a.len; i > 0; i--){
if (a.s[i] < b.s[i]) return true;
if (a.s[i] > b.s[i]) return false;
}
return false;
}
void Half(NUM& a){
int carry = 0,temp;
for (int i = a.len; i > 0; i--){
temp = (a.s[i] + carry * Base) / 2;
carry = a.s[i] + carry * Base - temp * 2;
a.s[i] = temp;
}
while (!a.s[a.len]) a.len--;
}
void Twice(NUM& a){
int carry = 0,temp;
for (int i = 1; i <= a.len; i++){
temp = a.s[i] * 2 + carry;
a.s[i] = temp % Base;
carry = temp / Base;
}
while (carry) a.s[++a.len] = carry % Base,carry /= Base;
}
NUM operator -(const NUM& a,const NUM& b){
NUM c; c.len = a.len;
int carry = 0,temp;
for (int i = 1; i <= a.len; i++){
temp = a.s[i] - b.s[i] + carry;
if (temp < 0) carry = -1,temp += Base;
else carry = 0;
c.s[i] = temp;
}
while (!c.s[c.len]) c.len--;
return c;
}
int main(){
NUM A,B; int cnt = 0;
cin>>A>>B;
while (!equal(A,B)){
if (check(A) && check(B)) Half(A),Half(B),cnt++;
else if (check(A)) Half(A);
else if (check(B)) Half(B);
else {
if (B < A) swap(A,B);
B = B - A;
}
}
while (cnt--) Twice(A);
cout<<A<<endl;
return 0;
}
BZOJ1876 [SDOI2009]SuperGCD 【高精 + GCD优化】的更多相关文章
- bzoj千题计划288:bzoj1876: [SDOI2009]SuperGCD
http://www.lydsy.com/JudgeOnline/problem.php?id=1876 高精压位GCD 对于 GCD(a, b) a>b 若 a 为奇数,b 为偶数,GCD ...
- [BZOJ1876][SDOI2009]superGCD(高精度)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1876 分析: 以为辗转相减会TLE呢……但是好像没这个数据……就这么水过去了…… 辗转 ...
- bzoj1876: [SDOI2009]SuperGCD
更相减损数. 上手就debug了3个小时,直接给我看哭了. 3个函数都写错了是什么感受? 乘2函数要从前往后乘,这样后面的数乘2进位以后不会干扰前面的数. 除2函数要从后往前除,这样前面的数借来的位不 ...
- 【BZOJ1876】[SDOI2009]SuperGCD(数论,高精度)
[BZOJ1876][SDOI2009]SuperGCD(数论,高精度) 题面 BZOJ 洛谷 题解 那些说数论只会\(gcd\)的人呢?我现在连\(gcd\)都不会,谁来教教我啊? 显然\(gcd\ ...
- 【学术篇】SDOI2009 SuperGCD
特别说明: 为了避免以后搬家时的麻烦, 这里的文章继续沿用csdn的风格和分类好了~ Emmmm这个题是一道高精度的模板题啊~ 既然是高精度的裸题, 那我们这些懒人当然是选择:用python啦~ 懒癌 ...
- 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 3287: Mato的刷屏计划 高精水题 && bzoj AC150
3287: Mato的刷屏计划 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 124 Solved: 43[Submit][Status] Desc ...
- BZOJ 1876: [SDOI2009]SuperGCD( 更相减损 + 高精度 )
更相减损,要用高精度.... --------------------------------------------------------------- #include<cstdio> ...
随机推荐
- Python3 利用pip安装BeautifulSoup4模块(Windows版)
一.找到Python3的安装文件夹 二.将路径复制 三.Windows10 打开Windows PowerShell(管理员).Windows 8.8.1.7使用cmd 切换到相应目录 四.此目录下的 ...
- 3,jieba gensim 最好别分家之最简单的相似度实现
简单的问答已经实现了,那么问题也跟着出现了,我不能确定问题一定是"你叫什么名字",也有可能是"你是谁","你叫啥"之类的,这就引出了人工智能 ...
- [Windows]_[C/C++]_[如何调试子进程]
场景 1.VC++ 的程序A在启动程序C时, 如果需要调试程序C的话一般有两种, 一种是通过菜单 调试->附加到进程的方式来调试程序, 缺点就是这个进程必须先启动, 但是一启动的话有可能就执行了 ...
- C++基础语言知识大汇总(不断更新!!!)
经过十天的时间,LITTLESUN做好了前期的工作,今天LITTLESUN就要在新地图里扬帆起航喽!!!(撒花) 简单的整理了一下这次启航准备好的物资.后面的航程中也会不断来补充这个小仓库哦!
- itop-4412开发板使用第一篇-信号量的学习使用
1. 本次基于itop-4412研究下Linux信号量的使用方法. 2. 创建信号量的函数,信号量的头文件在那个路径?编译应用程序的话,头文件有3个路径,内核源码头文件,交叉编译器头文件,ubuntu ...
- innodb_index_stats
mysql> select * from mysql.innodb_index_stats WHERE database_name='test' and table_name='recordsI ...
- Android ImageSwitcher 配合Picasso解决内存溢出(OOM)问题
最近项目中用到了 ImageSwitcher 来实现图片切换,使用起来很简单,但发现当图片比较大(超过了3M)时,程序出现了内存溢出(OOM)问题而崩溃了. 原因就是图片太大了,显示到 ImageVi ...
- 【IOI 2002/FJOI2019】任务安排(超级计算机)
题目 \(N\) 个任务排成一个序列在一台机器上等待完成(顺序不得改变),这 \(N\) 个任务被分成若干批,每批包含相邻的若干任务.从时刻 \(0\) 开始,这些任务被分批加工,第 \(i\) 个任 ...
- Selenium八大元素定位方式
1.根据id来定位: import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.sele ...
- 今日Linux
一.复习了vi 三个模式下的一些操作.贴上一些比较常用,个人觉得比较难记的操作.1.一般模式:h 光标向左移动一个字符j 光标向下移动一个字符K 光标向上移动一个字符l 光标向右移动一个 ...