题意:给定m,m = n * (n+1) / 2,计算n值。

思路:n = SQRT(m*2)

注意m很大,需要自己实现大数开方。我用的是自己写的大数模板:大数模板

AC代码

  1. #include <cstdio>
  2. #include <cmath>
  3. #include <algorithm>
  4. #include <cstring>
  5. #include <utility>
  6. #include <string>
  7. #include <iostream>
  8. #include <map>
  9. #include <set>
  10. #include <vector>
  11. #include <queue>
  12. #include <stack>
  13. using namespace std;
  14. #define eps 1e-10
  15. #define inf 0x3f3f3f3f
  16. #define PI pair<int, int>
  17. typedef long long LL;
  18. const int maxn = 1e4 + 5;
  19.  
  20. struct BigInteger {
  21. vector<int>s; //12345--54321
  22.  
  23. void DealZero() { //处理前导0
  24. for(int i = s.size() - 1; i > 0; --i){
  25. if(s[i] == 0) s.pop_back();
  26. else break;
  27. }
  28. }
  29.  
  30. BigInteger operator = (long long num) { // 赋值运算符
  31. s.clear();
  32. vector<int>tmp;
  33. do{
  34. s.push_back(num % 10);
  35. num /= 10;
  36. }while(num);
  37. return *this;
  38. }
  39.  
  40. BigInteger operator = (const string& str) { // 赋值运算符
  41. s.clear();
  42. for(int i = str.size() - 1; i >= 0; --i) s.push_back(str[i] - '0');
  43. this->DealZero();
  44. return *this;
  45. }
  46. BigInteger operator = (const char *a) {
  47. int n = strlen(a);
  48. }
  49.  
  50. BigInteger operator + (const BigInteger& b) const {
  51. BigInteger c;
  52. c.s.clear();
  53. int len1 = s.size(), len2 = b.s.size();
  54. for(int i = 0, g = 0; g > 0 || i < len1 || i < len2; ++i) {
  55. int x = g;
  56. if(i < len1) x += s[i];
  57. if(i < len2) x += b.s[i];
  58. c.s.push_back(x % 10);
  59. g = x / 10;
  60. }
  61. return c;
  62. }
  63.  
  64. //大数减小数
  65. BigInteger operator - (const BigInteger& b) const {
  66. BigInteger c;
  67. c.s.clear();
  68. int len1 = s.size(), len2 = b.s.size();
  69. for(int i = 0, g = 0; i < len1 || i < len2; ++i) {
  70. int x = g;
  71. if(i < len1) x += s[i];
  72. g = 0;
  73. if(i < len2) x -= b.s[i];
  74. if(x < 0) {
  75. g = -1; //借位
  76. x += 10;
  77. }
  78. c.s.push_back(x);
  79. }
  80. c.DealZero();
  81. return c;
  82. }
  83.  
  84. BigInteger operator * (const BigInteger& b) const {
  85. BigInteger c, tmp;
  86. c.s.clear();
  87. int len1 = s.size(), len2 = b.s.size();
  88. for(int i = 0; i < len1; ++i) {
  89. tmp.s.clear();tmp;
  90. int num = i;
  91. while(num--) tmp.s.push_back(0);
  92. int g = 0;
  93. for(int j = 0; j < len2; ++j) {
  94. int x = s[i] * b.s[j] + g;
  95. tmp.s.push_back(x % 10);
  96. g = x / 10;
  97. }
  98. if(g > 0) tmp.s.push_back(g);
  99. c = c + tmp;
  100. }
  101. c.DealZero();
  102. return c;
  103. }
  104.  
  105. //单精度除法
  106. BigInteger operator / (const int b) const {
  107. BigInteger c, tmp;
  108. c.s.clear();
  109. int len = s.size();
  110. int div = 0;
  111. for(int i = len - 1; i >= 0; --i) {
  112. div = div * 10 + s[i];
  113. while(div < b && i > 0) {
  114. div = div * 10 + s[--i];
  115. }
  116. tmp.s.push_back(div / b);
  117. div %= b;
  118. }
  119. for(int i = tmp.s.size() - 1; i >= 0; --i) c.s.push_back(tmp.s[i]);
  120. c.DealZero();
  121. return c;
  122. }
  123.  
  124. bool operator < (const BigInteger& b) const {
  125. int len1 = s.size(), len2 = b.s.size();
  126. if(len1 != len2) return len1 < len2;
  127. for(int i = len1 - 1; i >= 0; --i) {
  128. if(s[i] != b.s[i]) return s[i] < b.s[i];
  129. }
  130. return false; //相等
  131. }
  132.  
  133. bool operator <= (const BigInteger& b) const {
  134. return !(b < *this);
  135. }
  136. string ToStr() {
  137. string ans;
  138. ans.clear();
  139. for(int i = s.size()-1; i >= 0; --i)
  140. ans.push_back(s[i] + '0');
  141. return ans;
  142. }
  143.  
  144. //大数开方
  145. /**大数开方用法说明:
  146. 字符串必须从第二个位置开始输入,且s[0] = '0'
  147. scanf("%s", s+1);
  148. */
  149. BigInteger SQRT(char *s) {
  150. string p = "";
  151. s[0]='0';
  152. if(strlen(s)%2 == 1)
  153. work(p, 2, s+1, 0);
  154. else
  155. work(p, 2, s, 0);
  156. BigInteger c;
  157. c.s.clear();
  158. c = p;
  159. return c;
  160. }
  161.  
  162. //开方准备
  163. //------------------------------------
  164. int l;
  165. int work(string &p, int o,char *O,int I){
  166. char c, *D=O ;
  167. if(o>0)
  168. {
  169. for(l=0;D[l];D[l++]-=10)
  170. {
  171. D[l++]-=120;
  172. D[l]-=110;
  173. while(!work(p, 0, O, l))
  174. D[l]+=20;
  175. p += (char)((D[l]+1032)/20);
  176.  
  177. }
  178. }
  179. else
  180. {
  181. c=o+(D[I]+82)%10-(I>l/2)*(D[I-l+I]+72)/10-9;
  182. D[I]+=I<0 ? 0 : !(o=work(p, c/10,O,I-1))*((c+999)%10-(D[I]+92)%10);
  183. }
  184. return o;
  185. }
  186. //-----------------------------------------
  187. };
  188.  
  189. ostream& operator << (ostream &out, const BigInteger& x) {
  190. for(int i = x.s.size() - 1; i >= 0; --i)
  191. out << x.s[i];
  192. return out;
  193. }
  194.  
  195. istream& operator >> (istream &in, BigInteger& x) {
  196. string s;
  197. if(!(in >> s)) return in;
  198. x = s;
  199. return in;
  200. }
  201.  
  202. int main() {
  203. BigInteger a, tmp;
  204. tmp = 2;
  205. string str;
  206. char s[maxn];
  207. while(cin >> str) {
  208. a = str;
  209. a = tmp * a;
  210. int cur = 1;
  211. for(int i = a.s.size()-1; i >= 0; --i) {
  212. s[cur++] = a.s[i] + '0';
  213. }
  214. cout << a.SQRT(s) << "\n";
  215. }
  216. return 0;
  217. }

如有不当之处欢迎指出!

URAL - 1153 Supercomputer 大数开方的更多相关文章

  1. ural 1153. Supercomputer

    1153. Supercomputer Time limit: 2.0 secondMemory limit: 64 MB To check the speed of JCN Corporation ...

  2. Java中利用BigInteger类进行大数开方

    在Java中有时会用到大数据,基本数据类型的存储范围已经不能满足要求了,如要对10的1000次方的这样一个数据规模的数进行开方运算,很明显不能直接用Math.sqrt()来进行计算,因为已经溢出了. ...

  3. ACM-ICPC2018焦作网络赛 Participate in E-sports(大数开方)

    Participate in E-sports 11.44% 1000ms 65536K   Jessie and Justin want to participate in e-sports. E- ...

  4. 蓝桥杯T126(xjb&大数开方)

    题目链接:http://lx.lanqiao.cn/problem.page?gpid=T126 题意:中文题诶- 思路:显然被翻转了奇数次的硬币为反面朝上,但是本题的数据量很大,所以O(n^2)枚举 ...

  5. JAVA 大数开方模板

    JAVA 大数开方模板 import java.math.BigInteger; import java.math.*; import java.math.BigInteger; import jav ...

  6. 大数开方 ACM-ICPC 2018 焦作赛区网络预赛 J. Participate in E-sports

    Jessie and Justin want to participate in e-sports. E-sports contain many games, but they don't know ...

  7. ACM-ICPC 2018 焦作赛区网络预赛 J Participate in E-sports(大数开方)

    https://nanti.jisuanke.com/t/31719 题意 让你分别判断n或(n-1)*n/2是否是完全平方数 分析 二分高精度开根裸题呀.经典题:bzoj1213 用java套个板子 ...

  8. Very simple problem - SGU 111(大数开方)

    分析:使用的是构造新数字法进行不断构造,然后逼近每一位数字,然后使用c++徒手敲了240多行代码,竟然过了........................很有成就感. 代码如下: ========== ...

  9. 大数模板(Java)

    大数加法 /* 给出2个大整数A,B,计算A+B的结果. Input 第1行:大数A 第2行:大数B (A,B的长度 <= 10000 需注意:A B有可能为负数) Output 输出A + B ...

随机推荐

  1. 用SecureCRT来上传和下载文件

    用SSH管理linux服务器时经常需要远程与本地之间交互文件.而直接用SecureCRT自带的上传下载功能无疑是最方便的,SecureCRT下的文件传输协议有ASCII.Xmodem.Zmodem. ...

  2. Linux指令--rm, rmdir

    rm是常用的命令,该命令的功能为删除一个目录中的一个或多个文件或目录,它也可以将某个目录及其下的所有文件及子目录均删除.对于链接文件,只是删除了链接,原有文件均保持不变.rm是一个危险的命令,使用的时 ...

  3. java虚拟机和java内存区域概述

    什么是虚拟机,什么是Java虚拟机 虚拟机 定义:模拟某种计算机体系结构,执行特定指令集的软件 系统虚拟机(Virtual Box.VMware),进程虚拟机 进程虚拟机 jvm.Adobe Flas ...

  4. DNS入门

    引言 常见的计网协议通过IP地址来识别分布式应用的主机,然而IPV4(特别是IPV6)的地址太繁琐难以使用和记忆,因此提出了使用主机名称来识别,实质是:主机名称通过称为名称解析的过程转换为IP地址.其 ...

  5. maven的聚合和继承

    Maven的聚合特性能够把项目的各个模块聚合在一起构建: 而Maven的继承特性则能帮组抽取各模块相同的依赖和插件等配置,在简化POM的同时,还能促进各个模块配置的一致性. 聚合:新建一个项目demo ...

  6. SSE图像算法优化系列十五:YUV/XYZ和RGB空间相互转化的极速实现(此后老板不用再担心算法转到其他空间通道的耗时了)。

    在颜色空间系列1: RGB和CIEXYZ颜色空间的转换及相关优化和颜色空间系列3: RGB和YUV颜色空间的转换及优化算法两篇文章中我们给出了两种不同的颜色空间的相互转换之间的快速算法的实现代码,但是 ...

  7. lvs_dr

    lvs_dr 实验需求(4台虚拟机) eth0 192.168.1.110 单网卡 client(可以使用windows浏览器代替,但会有缓存影响) eth0 192.168.1.186 单网卡 di ...

  8. 让Python支持中文注释

    在第一行中加入如下行即可,表示文件的编码: #coding=utf-8 或 #coding=gbk

  9. PHP使用file_get_contents或curl请求https的域名内容为空或Http 505错误的问题排查方法

    前段日子,突然接到用户的反馈,说系统中原来的QQ登录.微博登录通通都不能用,跟踪代码进去后发现,是在 file_get_contents这个函数请求QQ登录的地方报错,在用该函数file_get_co ...

  10. CentOS(Linux)下安装dmidecode包

    安装代码: yum install dmidecode 安装完成后,查看总体信息: dmidecode 查看服务器类型,测试环境为DELL R610: dmidecode -s system-prod ...