hdu 5050 大数
http://acm.hdu.edu.cn/showproblem.php?pid=5050
大数模板最大公约数
信kuangbin,能AC
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define clr0(x) memset(x,0,sizeof(x)) const int maxn = 1010; struct Num
{
int num[1100];
}; void print(const Num &hold)
{
int k; for (k = maxn; k >= 0; k --)
if (hold.num[k])
break; for (int i = k; i >= 0; i --)
printf("%d", hold.num[i]);
} Num operator+(const Num &a, const Num &b)
{
Num ret;
int temp; clr0(ret.num); for (int i = 0; i <= maxn; i ++)
{
temp = ret.num[i]; ret.num[i] = (temp + a.num[i] + b.num[i]) % 2;
ret.num[i + 1] = (temp + a.num[i] + b.num[i]) / 2;
} return ret;
} bool operator==(const Num &a, int num)
{
for (int i = 0; i <= maxn; i ++)
if (a.num[i] != num)
return false; return true;
} bool operator<(const Num &a, const Num &b)
{
for (int i = maxn; i >= 0; i --)
{
if (a.num[i] < b.num[i])
return true;
else if (a.num[i] > b.num[i])
return false;
} return true;
} Num Complement(Num hold)
{
char str[1100];
Num ret, temp; strcpy(str, "1"); clr0(temp.num);
int bit = 0,lls = strlen(str);
for (int i = lls - 1; i >= 0; i --)
temp.num[bit ++] = str[i] - '0'; ret = hold; for (int i = 0; i <= maxn; i ++)
{
if (ret.num[i])
ret.num[i] = 0;
else
ret.num[i] = 1;
} ret = ret + temp; return ret;
} Num operator-(const Num &a, const Num &b)
{
Num c, ret; c = Complement(b); ret = a + c; return ret;
} Num operator/(const Num &hold, int num)
{
Num ret; clr0(ret.num); for (int i = 0; i < maxn; i ++)
ret.num[i] = hold.num[i + 1]; return ret;
} int operator%(const Num &hold, int num)
{
return hold.num[0];
} Num gcd(Num a, Num b, int &counter)
{
while (true)
{
if (a < b)
swap(a, b); if (b == 0)
break; if (a % 2 == 0 && b % 2 == 0)
{
counter ++; a = a / 2;
b = b / 2;
}
else if (a % 2 == 0 && b % 2 != 0)
{
a = a / 2;
b = b;
}
else if (a % 2 != 0 && b % 2 == 0)
{
a = a;
b = b / 2;
}
else
{
a = a - b;
b = b;
}
} return a;
} int main()
{
int _;RD(_);
char ta[1100], tb[1100];
int counter;
Num a, b, ans ;
int bit,cas = 1;
while (_--)
{
printf("Case #%d: ",cas++);
scanf("%s%s", ta, tb); int lla = strlen(ta),llb = strlen(tb); clr0(a.num);
bit = 0;
for (int i = lla - 1; i >= 0; i --)
a.num[bit ++] = ta[i] - '0';
clr0(b.num);
bit = 0;
for (int i = llb - 1; i >= 0; i --)
b.num[bit ++] = tb[i] - '0'; counter = 0;
ans = gcd(a, b, counter); print(ans); while(counter--)
printf("0"); puts("");
} return 0;
}
hdu 5050 大数的更多相关文章
- HDU 5050
http://acm.hdu.edu.cn/showproblem.php?pid=5050 大数gcd import java.io.* ; import java.math.* ; import ...
- HDU - 5050 (大数二进制gcd)
It's time to fight the local despots and redistribute the land. There is a rectangular piece of land ...
- hdu 5050 java程序求大数最大公约数
import java.io.*; import java.math.*; import java.util.*; import java.text.*; public class Main { pu ...
- hdu 1002大数(Java)
A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdu 5047 大数找规律
http://acm.hdu.edu.cn/showproblem.php?pid=5047 找规律 信kuangbin,能AC #include <stdio.h> #include & ...
- hdu 4759 大数+找规律 ***
题目意思很简单. 就是洗牌,抽出奇数和偶数,要么奇数放前面,要么偶数放前面. 总共2^N张牌. 需要问的是,给了A X B Y 问经过若干洗牌后,第A个位置是X,第B个位置是Y 是不是可能的. Ja ...
- HDU 1018 大数(求N!的位数/相加)
Big Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- HDU 4927 大数运算
模板很重要 #include <cstdio> #include <cstring> #include <cstdlib> #include <iostrea ...
- HDU 5050 Divided Land(进制转换)
题意 给你两个二进制数m,n 求他们的最大公约数 用二进制表示 0<m,n<2^1000 先把二进制转换为十进制 求出最大公约数 再把结果转换为二进制 数比較大要用到大数 ...
随机推荐
- 计算器类(C++&JAVA——表达式转换、运算、模板公式)
运行: (a+b)*c 后缀表达式:ab+c* 赋值: Enter the a : 10 Enter the b : 3 Enter the c : 5 结果为:65 代码是我从的逻辑判断系统改过来的 ...
- hdu (欧拉函数+容斥原理) GCD
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1695 看了别人的方法才会做 参考博客http://blog.csdn.net/shiren_Bod/ar ...
- BZOJ1093或洛谷2272 [ZJOI2007]最大半连通子图
BZOJ原题链接 洛谷原题链接 和 Going from u to v or from v to u?(题解)这道题类似,只不过是求最大子图的大小和个数而已. 一样用\(tarjan\)求强连通分量, ...
- JoyOI1035 棋盘覆盖
原题链接 对棋盘染色,坐标和为奇数的染黑,偶数为白.这时会发现对于相同颜色的格子,是无法放置骨牌的,这样我们就将所有格子分成两类,然后根据能否放置骨牌连边,最后就是求二分图最大匹配了. 这里我是用的匈 ...
- tiny cc 编译器,tinycc,变种
去掉了 -run 参数 下载代码和编译好的程序
- 拍照一分钟,修图两小时,PS大神是这样修片的!
乌克兰有一个叫Viktoria Solidarnyh的美图艺术家,这个艺术家有一个特别的技能——P图,她P的图,水平真的非常赞...来感受一下.... 瞬间变成魔幻田园风... 编辑:千锋UI设计 ...
- wordvec_词的相似度
import gensimfrom gensim.models import word2vecimport loggingimport jiebaimport osimport numpy as np ...
- Vim on Mac Terminal
2018-04-15 在Python 里面加标注, 发现Vim强大的两种用法, 比如要在1-5行加标注: 1. 用寻找和替代(basic search and replace),:1, 5s/^/# ...
- ros 编程习惯
1.设置ros的info,warning,debug,error等编写的时候要思考,何时该使用,以及在开头要使用设置rosconsole的级别来对应输出,以方便调试. 2.在使用ros_info等的时 ...
- SpringMVC学习笔记:表单提交 参数的接收
SpringMVC可以接收原生form表单和json格式数据 有一个名为Book的model,其中的属性如下: 字符串类型的name,数字类型的price,数组类型的cover,集合类型的author ...