1. /*
  2. POJ2389 Bull Math
  3. http://poj.org/problem?id=2389
  4. 高精度乘法
  5. *
  6. */
  7. #include <cstring>
  8. #include <cstdio>
  9. #include <algorithm>
  10. using namespace std;
  11. const int Nmax=;
  12. struct BigInt
  13. {
  14. int a[Nmax];
  15. int n;
  16. void init()
  17. {
  18. for(int i=;i<Nmax;i++)
  19. a[i]=;
  20. }
  21. BigInt()
  22. {
  23. init();
  24. n=;
  25. }
  26. BigInt(int _a[],int _n)
  27. {
  28. init();
  29. n=_n;
  30. //for(int i=0;i<Nmax;i++)
  31. //a[i]=0;
  32. for(int i=;i<=n;i++)
  33. a[i]=_a[i];
  34. maintain();
  35. }
  36. BigInt(char s[])
  37. {
  38. init();
  39. n=strlen(s+);
  40. for(int i=;i<=n;i++)
  41. a[i]=s[n-i+]-'';
  42. maintain();
  43. }
  44. BigInt(long long x)
  45. {
  46. init();
  47. n=;
  48. while(x>0LL)
  49. {
  50. a[++n]=x%10LL;
  51. x/=10LL;
  52. }
  53. }
  54. BigInt(int x)
  55. {
  56. init();
  57. n=;
  58. while(x>)
  59. {
  60. a[++n]=x%;
  61. x/=;
  62. }
  63. }
  64. void read()
  65. {
  66. init();
  67. n=;
  68. char c=getchar();
  69. if(c==-)
  70. return;
  71. while(c==' ' || c=='\n' )
  72. {
  73. c=getchar();
  74. if(c==-)
  75. break;
  76. }
  77. int num[Nmax];
  78. while(c!=' ' && c!='\n' && c!=-)
  79. {
  80. num[++n]=c-'';
  81. c=getchar();
  82. }
  83. for(int i=;i<=n;i++)
  84. a[i]=num[n-i+];
  85. maintain();
  86. }
  87. void print()
  88. {
  89. if(n==)
  90. printf("");
  91. for(int i=n;i>=;i--)
  92. printf("%d",a[i]);
  93. }
  94. void maintain()
  95. {
  96. for(int i=;i<=n;i++)
  97. {
  98. a[i+]+=a[i]/;
  99. a[i]%=;
  100. }
  101. int j=n+;
  102. while(a[j]!=)
  103. {
  104. a[j+]+=a[j]/;
  105. a[j]%=;
  106. j++;
  107. }
  108. n=j-;
  109. while(a[n]== && n>)
  110. n--;
  111. }
  112. friend BigInt operator + (BigInt a,BigInt b)
  113. {
  114. int len=max(a.n,b.n);
  115. BigInt ans;
  116. ans.n=len;
  117. for(int i=;i<=len;i++)
  118. ans.a[i]=a.a[i]+b.a[i];
  119. ans.maintain();
  120. return ans;
  121. }
  122. //friend BigInt operator - (BigInt a,BigInt b)
  123. //{
  124. //int len=max(a.n,b.n);
  125. //BigInt ans;
  126. //ans.n=len;
  127. //for(int i=1;i<=len;i++)
  128. //ans.a[i]=a.a[i]-b.a[i];
  129. //for(int i=1;i<=len;i++)
  130. //{
  131. //if(ans.a[i]<0)
  132. //{
  133. //ans.a[i]+=10;
  134. //a.a[i+1]--;
  135. //}
  136. //ans.a[i]+=10
  137. //}
  138. //}
  139. friend BigInt operator * (BigInt b,int a)
  140. {
  141. int n=b.n;
  142. BigInt ans;
  143. ans.n=n;
  144. for(int i=;i<=n;i++)
  145. ans.a[i]=b.a[i]*a;
  146. ans.maintain();
  147. return ans;
  148. }
  149. friend BigInt operator * (int a,BigInt b)
  150. {
  151. int n=b.n;
  152. BigInt ans;
  153. ans.n=n;
  154. for(int i=;i<=n;i++)
  155. ans.a[i]=b.a[i]*a;
  156. ans.maintain();
  157. return ans;
  158. }
  159. friend BigInt operator * (BigInt a,BigInt b)
  160. {
  161. BigInt ans;
  162. ans.n=a.n+b.n+;
  163. int k;
  164. for(int i=;i<=a.n;i++)
  165. for(int j=;j<=b.n;j++)
  166. ans.a[j+i-]+=a.a[i]*b.a[j];
  167. ans.maintain();
  168. return ans;
  169. }
  170. };
  171. int main()
  172. {
  173. char s[];
  174. char s1[];
  175. BigInt a,b;
  176. //freopen("test.in","r",stdin);
  177. a.read();
  178. b.read();
  179. (a*b).print();
  180. printf("\n");
  181. return ;
  182. }

POJ2389 Bull Math的更多相关文章

  1. POJ2389 Bull Math【大数】

    Bull Math Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15040   Accepted: 7737 Descri ...

  2. Poj OpenJudge 百练 2389 Bull Math

    1.Link: http://poj.org/problem?id=2389 http://bailian.openjudge.cn/practice/2389/ 2.Content: Bull Ma ...

  3. BZOJ1754: [Usaco2005 qua]Bull Math

    1754: [Usaco2005 qua]Bull Math Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 374  Solved: 227[Submit ...

  4. 1754: [Usaco2005 qua]Bull Math

    1754: [Usaco2005 qua]Bull Math Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 398  Solved: 242[Submit ...

  5. POJ 2389 Bull Math(水~Java -大数相乘)

    题目链接:http://poj.org/problem?id=2389 题目大意: 大数相乘. 解题思路: java BigInteger类解决 o.0 AC Code: import java.ma ...

  6. BZOJ 1754: [Usaco2005 qua]Bull Math

    Description Bulls are so much better at math than the cows. They can multiply huge integers together ...

  7. [PKU2389]Bull Math (大数运算)

    Description Bulls are so much better at math than the cows. They can multiply huge integers together ...

  8. poj 2389.Bull Math 解题报告

    题目链接:http://poj.org/problem?id=2389 题目意思:就是大整数乘法. 题目中说每个整数不超过 40 位,是错的!!!要开大点,这里我开到100. 其实大整数乘法还是第一次 ...

  9. 【BZOJ】1754: [Usaco2005 qua]Bull Math

    [算法]高精度乘法 #include<cstdio> #include<algorithm> #include<cstring> using namespace s ...

随机推荐

  1. ubuntu14.04 安装LNMP

    新书上市<深入解析Android 5.0系统> 通常我们使用centos来组建LNMP,可是我们开发时多使用ubuntu的桌面版本号来调试,以下将具体介绍怎样在ubuntu上安装一套LNM ...

  2. 轻快的VIM(五):复制

    操作相同文本的时候复制尤其有效,在Windows中我们都习惯了先用鼠标选择文本 而Vim下则不用那么麻烦,你甚至可以使用可视模式操作,但这里先略过 我在这一节主要说说命令模式下的复制 在讲复制之前我要 ...

  3. 0x59 单调队列优化DP

    倍增DP太难啦心情好再回去做 poj1821 先让工匠按s排序,f[i][j]表示枚举到第i个工匠涂了j个木板(注意第j个木板不一定要涂) 那么f[i][j]可以直接继承f[i-1][j]和f[i][ ...

  4. ReflectionSugar 通用反射类

    http://www.cnblogs.com/sunkaixuan/p/4635710.html

  5. js 对象方法、类方法、原型方法区别

    function People(name){this.name=name;//对象方法this.Introduce=function(){alert("My name is "+t ...

  6. java中的访问修饰符2

    综上所述:protected强调的是子类,deafult强调的是本包,private强调的是本类,public强调的是开放性.

  7. 大数据查询——HBase读写设计与实践--转

    背景介绍 本项目主要解决 check 和 opinion2 张历史数据表(历史数据是指当业务发生过程中的完整中间流程和结果数据)的在线查询.原实现基于 Oracle 提供存储查询服务,随着数据量的不断 ...

  8. 豆瓣项目(用react+webpack)

    用豆瓣电影api的项目 电影列表组件渲染 步骤: 1. 发送Ajax请求 1.1 在组件的componentWillMount这个生命周期钩子中发送请求 1.2 发送ajax XMLHttpReque ...

  9. rem简单实现移动端适配

    rem:移动web开发 默认字体大小是16px 在<html>中设置字体大小 与em的区别: em是在父级设置字体大小受影响 移动端适配 首先获取屏幕的宽度 计算当前屏幕宽度和640的比例 ...

  10. Android 拍照图片选取与图片剪裁

    最近从以前的项目中扒下来一个常用的模块,在这里有必要记录一下的,就是android上获取图片以及裁剪图片,怎么样?这个功能是不是很常用啊,你随便打开一个App,只要它有注册功能都会有设置人物头像的功能 ...