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

思路:n = SQRT(m*2)

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

AC代码

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<int, int>
typedef long long LL;
const int maxn = 1e4 + 5;

struct BigInteger {
	vector<int>s;  //12345--54321

	void DealZero() { //处理前导0
		for(int i = s.size() - 1; i > 0; --i){
			if(s[i] == 0) s.pop_back();
			else break;
		}
	}

 	BigInteger operator = (long long num) { // 赋值运算符
 		s.clear();
 		vector<int>tmp;
       	do{
       		s.push_back(num % 10);
       		num /= 10;
		}while(num);
		return *this;
    }

	BigInteger operator = (const string& str) { // 赋值运算符
    	s.clear();
   		for(int i = str.size() - 1; i >= 0; --i) s.push_back(str[i] - '0');
   		this->DealZero();
   		return *this;
    }
    BigInteger operator = (const char *a) {
    	int n = strlen(a);
	}

    BigInteger operator + (const BigInteger& b) const {
    	BigInteger c;
    	c.s.clear();
    	int len1 = s.size(), len2 = b.s.size();
    	for(int i = 0, g = 0; g > 0 || i < len1 || i < len2; ++i) {
    		int x = g;
    		if(i < len1) x += s[i];
    		if(i < len2) x += b.s[i];
    		c.s.push_back(x % 10);
    		g = x / 10;
		}
    	return c;
   }

   //大数减小数
   BigInteger operator - (const BigInteger& b) const {
    	BigInteger c;
    	c.s.clear();
    	int len1 = s.size(), len2 = b.s.size();
    	for(int i = 0, g = 0; i < len1 || i < len2; ++i) {
    		int x = g;
    		if(i < len1) x += s[i];
    		g = 0;
    		if(i < len2) x -= b.s[i];
    		if(x < 0) {
    			g = -1; //借位
    			x += 10;
			}
    		c.s.push_back(x);
		}
		c.DealZero();
    	return c;
   }

   BigInteger operator * (const BigInteger& b) const {
   		BigInteger c, tmp;
    	c.s.clear();
    	int len1 = s.size(), len2 = b.s.size();
   		for(int i = 0; i < len1; ++i) {
   			tmp.s.clear();tmp;
   			int num = i;
			while(num--) tmp.s.push_back(0);
			int g = 0;
			for(int j = 0; j < len2; ++j) {
				int x = s[i] * b.s[j] + g;
				tmp.s.push_back(x % 10);
				g = x / 10;
			}
			if(g > 0) tmp.s.push_back(g);
			c = c + tmp;
		}
		c.DealZero();
		return c;
   }

   //单精度除法
   BigInteger operator / (const int b) const {
   		BigInteger c, tmp;
   		c.s.clear();
   		int len = s.size();
   		int div = 0;
   		for(int i = len - 1; i >= 0; --i) {
   			div = div * 10 + s[i];
   			while(div < b && i > 0) {
   				div = div * 10 + s[--i];
			}
			tmp.s.push_back(div / b);
			div %= b;
		}
		for(int i = tmp.s.size() - 1; i >= 0; --i) c.s.push_back(tmp.s[i]);
		c.DealZero();
		return c;
   } 

   bool operator < (const BigInteger& b) const {
   	   	int len1 = s.size(), len2 = b.s.size();
   	   	if(len1 != len2) return len1 < len2;
   	   	for(int i = len1 - 1; i >= 0; --i) {
   	   		if(s[i] != b.s[i]) return s[i] < b.s[i];
		}
		return false; //相等
   }

   bool operator <= (const BigInteger& b) const {
   		return !(b < *this);
   }
   string ToStr() {
   		string ans;
   		ans.clear();
   		for(int i = s.size()-1; i >= 0; --i)
   			ans.push_back(s[i] + '0');
   		return ans;
   }

   //大数开方
/**大数开方用法说明:
   字符串必须从第二个位置开始输入,且s[0] = '0'
   scanf("%s", s+1);
*/
   BigInteger SQRT(char *s) {
   		string p = "";
		s[0]='0';
    	if(strlen(s)%2 == 1)
        	work(p, 2, s+1, 0);
    	else
        	work(p, 2, s, 0);
   		BigInteger c;
   		c.s.clear();
   		c = p;
   		return c;
   }

//开方准备
//------------------------------------
	int l;
	int work(string &p, int o,char *O,int I){
	    char c, *D=O ;
	    if(o>0)
	    {
	        for(l=0;D[l];D[l++]-=10)
	        {
	            D[l++]-=120;
	            D[l]-=110;
	            while(!work(p, 0, O, l))
	                D[l]+=20;
	            p += (char)((D[l]+1032)/20);

	        }
	    }
	    else
	    {
	        c=o+(D[I]+82)%10-(I>l/2)*(D[I-l+I]+72)/10-9;
	        D[I]+=I<0 ? 0 : !(o=work(p, c/10,O,I-1))*((c+999)%10-(D[I]+92)%10);
	    }
	    return o;
	}
//-----------------------------------------
};

ostream& operator << (ostream &out, const BigInteger& x) {
	for(int i = x.s.size() - 1; i >= 0; --i)
		out << x.s[i];
	return out;
}

istream& operator >> (istream &in, BigInteger& x) {
  string s;
  if(!(in >> s)) return in;
  x = s;
  return in;
}

int main() {
	BigInteger a, tmp;
	tmp = 2;
	string str;
	char s[maxn];
	while(cin >> str) {
		a = str;
		a = tmp * a;
		int cur = 1;
		for(int i = a.s.size()-1; i >= 0; --i) {
			s[cur++] = a.s[i] + '0';
		}
		cout << a.SQRT(s) << "\n";
	}
	return 0;
}

如有不当之处欢迎指出!

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. python_5_模块

    创:5_4_2017 修: 什么是模块? --标准库+第三方库+自定义,为实现某一方面的功能集合(变量,函数,类) 如何安装第三方库? --pip install 第三方库 如何导入和使用模块? -- ...

  2. Oracle通过JOB定时执行存储过程实现两表数据比对

    需求: 第三方云平台管理的虚拟机会进行关机.资源扩展等操作,因此开关机状态.CPU.内存.磁盘大小等数据需要进行同步.这里第三方云平台是BMC CLM云平台,底层虚拟化平台是Vcenter.进行同步的 ...

  3. struts2 action 页面与action参数的传递的三种方式

    第一种: 初始页面: <form action="LoginAction.action" method="post"> 用户名:<input ...

  4. awk之NR==FNR问题

    NR,表示awk开始执行程序后所读取的数据行数. FNR,与NR功用类似,不同的是awk每打开一个新文件,FNR便从0重新累计. 下面看两个例子: 1,对于单个文件NR 和FNR 的 输出结果一样的 ...

  5. 浅谈TreeMap以及在java中的使用

    treemap结构是红黑树 1.先介绍一下平衡二叉树 其特点是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树.也就是说该二叉树的任何一个子节点,其左右子树的高度 ...

  6. 使用CefSharp开发一个12306“安心刷票弹窗通知”工具

    有需求就要改进 最近两年没有在春节回家过年了,主要是票太难买,虽然之前写了一个12306“无声购票弹窗”工具,解决了抢票问题,但是全家老小一起回去还是很累,干脆就在北京过年了.这两天突然有一个朋友问我 ...

  7. dubbo扩展http协议后FullGC

    问题 dubbo内部定制的版本中,在处理大于10K的包的时候,会出现内存溢出的现象 原因是我们在定制dubbo http协议的时候,使用了jboss包里面的HttpRequestDecoder的htt ...

  8. 机器学习01:使用scikit-learn的线性回归预测Google股票

    这是机器学习系列的第一篇文章. 本文将使用Python及scikit-learn的线性回归预测Google的股票走势.请千万别期望这个示例能够让你成为股票高手.下面按逐步介绍如何进行实践. 准备数据 ...

  9. Perf工具

    前段时间Linux下用nmon监控程序的运行,发现CPU的使用率很高,系统态Sys的比例很高.程序的速度不是很快,怀疑和上面的原因有关. 分别使用perf record,perf report和top ...

  10. web.xml组件加载顺序

    在配置项目组件的过程中, 了解Tomcat加载组件顺序很有必要. 例如某些框架如Quartz的集群功能需要数据库的支持, 数据库的加载肯定要在框架组件加载之前. 经过查阅和Debug发现, web.x ...