更相减损数。

上手就debug了3个小时,直接给我看哭了。

3个函数都写错了是什么感受?

乘2函数要从前往后乘,这样后面的数乘2进位以后不会干扰前面的数。

除2函数要从后往前除,这样前面的数借来的位不会除2次。

然后函数里面俩个类里面的变量名不要打混。

哭瞎

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxl = 2000 + 10;
const int d[]={10000000,1,10,100,1000,10000,100000,1000000};
char s[20010];
int cnt; struct bigint {
int a[maxl],len; int& operator [] (int x) {
return a[x];
} void init(char s[]) {
int n=strlen(s+1);
len=(n+7)/8;
for(int i=1,t,e;i<=n;i++) {
e=i%8;
t=(i+7)/8;
a[t]+=(s[n-i+1]-'0')*d[e];
}
} void print() {
printf("%d",a[len]);
for(int i=len-1;i>=1;i--) printf("%08d",a[i]);
printf("\n");
} void div() {
for(int i=1;i<=len;i++) {
if(a[i]&1) a[i-1]+=50000000;
a[i]/=2;
}
if(a[len]==0) len--;
a[0]=0;
} void mul() {
for(int i=len;i>0;i--) {
a[i]<<=1;
a[i+1]+=a[i]/100000000;
a[i]%=100000000;
}
while(a[len+1]) len++;
} bigint operator - (bigint b) {
bigint c;
for(int i=1;i<=len;i++) {
c[i]+=a[i]-b[i];
if(c[i]<0) {c[i]+=100000000; c[i+1]--;}
}
c.len=len;
while(!c[c.len] && c.len) c.len--;
return c;
} bool operator < (bigint b) {
if(len != b.len) {
if(len > b.len ) return 0;
else return 1;
} for(int i=len;i>=1;i--) {
if(a[i] != b[i]) {
if(a[i]<b[i]) return 1;
else return 0;
}
}
return 0;
} bigint() {
memset(a,0,sizeof(a));
len=0;
}
}a,b; int main() {
scanf("%s",s+1);
a.init(s);
scanf("%s",s+1);
b.init(s);
cnt=0;
while(1) {
while(!(a[1]&1) && !(b[1]&1)) {cnt++; a.div(); b.div();}
while(!(a[1]&1)) a.div();
while(!(b[1]&1)) b.div(); if(b<a) a=a-b;
else {
b=b-a;
if(b.len==0) {
while(cnt--) a.mul();
a.print();
break;
}
}
}
return 0;
}

bzoj1876: [SDOI2009]SuperGCD的更多相关文章

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

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

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

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

  3. BZOJ1876 [SDOI2009]SuperGCD 【高精 + GCD优化】

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

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

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

  5. BZOJ 1876: [SDOI2009]SuperGCD

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

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

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

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

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

  8. 【bzoj1876】[SDOI2009]SuperGCD(高精度)

    题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=1876 一道简单的高精度+Stein算法(或者叫辗转相除法)求最大公约数板子题. md还 ...

  9. BZOJ1876:[SDOI2009]SuperGCD——C++高精度良心题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1876 Description Sheng bill有着惊人的心算能力,甚至能用大脑计算出两个巨大的数 ...

随机推荐

  1. sql行转列和列转行(转)

    行列互转,是一个经常遇到的需求.实现的方法,有case when方式和2005之后的内置pivot和unpivot方法来实现. 在读了技术内幕那一节后,虽说这些解决方案早就用过了,却没有系统性的认识和 ...

  2. Apache Spark探秘:三种分布式部署方式比较

    转自:链接地址: http://dongxicheng.org/framework-on-yarn/apache-spark-comparing-three-deploying-ways/     目 ...

  3. c++ memset 函数 及 坑

    #include <string.h> #include <stdio.h> typedef struct ss{ int num; ][]; }tent; tent a; i ...

  4. 输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。

    // ConsoleApplication2.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include "stdafx.h ...

  5. centos6.5安装vmware-tools

    一.问题描述 为什么使用vmware-tools:传文件方便,可以从主机中直接拖拽文件到centos中(但实际上我们可以用:WinSCP):鼠标切换方便,鼠标指到什么地方,表名当前正在操作哪个环境(但 ...

  6. [设计模式] 10 外观模式 facade

    外观模式应该是用的很多的一种模式,特别是当一个系统很复杂时,系统提供给客户的是一个简单的对外接口,而把里面复杂的结构都封装了起来.客户只需使用这些简单接口就能使用这个系统,而不需要关注内部复杂的结构. ...

  7. 《head first java 》读书笔记(四)

    Updated 2014/04/09 P518--P581 <数据结构> ArrayList不能排序:TreeSet以有序状态保持并可防止重复.HashMap可用成对的name/value ...

  8. 无法将 lambda 表达式 转换为类型“System.Delegate”,因为它不是委托类型

    this.BeginInvoke(() => { this.btnQuery.Enabled = false; //禁用查询 }); 跨线程调用时,编译上面的代码将提示 对于Control.In ...

  9. linux sort 命令详解(转 )

    linux sort 命令详解 sort是在Linux里非常常用的一个命令,管排序的,集中精力,五分钟搞定sort,现在开始! 1 sort的工作原理 sort将文件的每一行作为一个单位,相互比较,比 ...

  10. POJ 1125 Stockbroker Grapevine(floyd)

    http://poj.org/problem?id=1125 题意 : 就是说想要在股票经纪人中传播谣言,先告诉一个人,然后让他传播给其他所有的经纪人,需要输出的是从谁开始传播需要的时间最短,输出这个 ...