高精度模板(From JCVB)
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<bitset>
#define cl(a) memset(a,0,sizeof(a))
using namespace std;
typedef long long ll;
typedef double db;
const db pi=3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862;
void gn(int &x){
int sg=;char c;while(((c=getchar())<''||c>'')&&c!='-');
if(c=='-')sg=-,x=;else x=c-'';
while((c=getchar())>=''&&c<='')x=x*+c-'';
x*=sg;
}
void gn(ll &x){
int sg=;char c;while(((c=getchar())<''||c>'')&&c!='-');
if(c=='-')sg=-,x=;else x=c-'';
while((c=getchar())>=''&&c<='')x=x*+c-'';
x*=sg;
}
int mo=;
int inf=;
db eps=1e-;
//ll inf=1000000000000000000ll;
int qp(int a,ll b){int ans=;do{if(b&)ans=1ll*ans*a%mo;a=1ll*a*a%mo;}while(b>>=);return ans;}
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int dx[]={,,-,};
int dy[]={,,,-};
#define x1 x192837465
#define x2 x123456789
#define y1 y192837465
#define y2 y123456789 #include<cstdio>
#include<vector>
#include<algorithm>
using namespace std; // upd 2015.11.28
class Bignum{
private:
vector<int>a;
void clear(){a.resize();a[]=;}
void set(int n){
if(clear(),n){
int t;a.clear();
while(n)a.push_back(n-(t=n/)*),n=t;
}
}
void set(long long n){
if(clear(),n){
long long t;a.clear();
while(n)a.push_back(n-(t=n/)*),n=t;
}
}
void clearzero(){
int l=a.size();
while(l> && a[l-]==)l--;
a.resize(l);
}
void set(char*st){
char c;vector<int> s;a.clear();
while(((c=*(st++))<'' || c>''));s.push_back(c-'');
while((c=*(st++))>=''&&c<='')s.push_back(c-'');
int l=s.size();
for (int i=l-;i->=;i-=)a.push_back(s[i]+s[i-]*+s[i-]*+s[i-]*);
if(l&){
int tmp=;
for (int i=;i<(l&);i++)tmp=tmp*+s[i];
a.push_back(tmp);
}
clearzero();
}
public:
Bignum(){clear();}
Bignum(int n){set(n);}
Bignum(long long n){set(n);}
Bignum(char*s){set(s);}
Bignum& operator=(int a){set(a);return *this;}
Bignum& operator=(long long a){set(a);return *this;}
Bignum& operator=(char *s){set(s);return *this;}
int size()const{
if(a.back()>=)return a.size()<<;
else if(a.back()>=)return (a.size()<<)-;
else if(a.back()>=)return (a.size()<<)-;
else return (a.size()<<)-;
}
long long toint()const{
long long ans=;
for (int i=int(a.size())-;i>=;i--)ans=ans*+a[i];
return ans;
}
int tostr(char *s)const{
int tot=;
for (unsigned int i=;i<a.size();i++)
if(i+==a.size()){
int x=a[i];
while(x)s[tot++]=''+x%,x/=;
}else{
int x=a[i];
for (int j=;j<;j++)s[tot++]=''+x%,x/=;
}
if(tot==)s[tot++]='';
for (int i=,j=tot-;i<j;i++,j--)swap(s[i],s[j]);
s[tot]=;
return tot;
}
int toarr(int *s)const{
int tot=;
for (unsigned int i=;i<a.size();i++)
if(i+==a.size()){
int x=a[i];
while(x)s[tot++]=x%,x/=;
}else{
int x=a[i];
for (int j=;j<;j++)s[tot++]=x%,x/=;
}
if(tot==)s[tot++]=;
return tot;
}
int comp(const Bignum&x)const{
if(a.size()<x.a.size())return ;
if(a.size()>x.a.size())return -;
for (int i=a.size()-;i>=;i--)
if(a[i]<x.a[i])return ;
else if(a[i]>x.a[i])return -;
return ;
}
bool operator<(const Bignum&x)const{return comp(x)==;}
bool operator>(const Bignum&x)const{return comp(x)==-;}
bool operator<=(const Bignum&x)const{return comp(x)!=-;}
bool operator>=(const Bignum&x)const{return comp(x)!=;}
bool operator!=(const Bignum&x)const{return comp(x)!=;}
bool operator==(const Bignum&x)const{return comp(x)==;}
Bignum& operator+=(const Bignum&x){
int l=max(a.size(),x.a.size())+;
a.resize(l);
for (unsigned int i=;i<x.a.size();i++)a[i]+=x.a[i];
for (int i=;i<l;i++)if(a[i]>=)a[i]-=,a[i+]++;
clearzero();return *this;
}
Bignum& operator-=(const Bignum&x){
for (unsigned int i=;i<x.a.size();i++)a[i]-=x.a[i];
for (unsigned int i=;i<a.size();i++)if(a[i]<)a[i]+=,a[i+]--;
clearzero();return *this;
}
Bignum operator+(const Bignum&x)const{Bignum y=*this;y+=x;return y;}
Bignum operator-(const Bignum&x)const{Bignum y=*this;y-=x;return y;}
Bignum operator*(const Bignum&x)const{
int l=a.size()+x.a.size();
Bignum z;z.a.clear();
vector<long long>tmp;tmp.resize(l);
for (unsigned int i=;i<x.a.size();i++)
for (unsigned int j=;j<a.size();j++)
tmp[i+j]+=x.a[i]*a[j];
for (int i=;i<l;i++){
long long t=tmp[i]/;
z.a.push_back(int(tmp[i]-t*));
t?tmp[i+]+=t:;
}
z.clearzero();return z;
}
Bignum operator*(long long x)const{
if(x>=)return *this * Bignum(x);
int l=a.size()+;
Bignum z;z.a.resize(l);
for (unsigned int i=;i<a.size();i++)z.a[i]=a[i]*x;
for (int i=,t;i<l;i++)z.a[i+]+=(t=z.a[i]/),z.a[i]-=t*;
z.clearzero();return z;
}
friend Bignum operator*(long long x,const Bignum&a){return a*x;}
Bignum& operator*=(long long x){return *this=*this*x;}
Bignum& operator*=(const Bignum&x){return *this=*this*x;}
Bignum operator/(const Bignum &x)const{
Bignum b=*this,q,tmp;q.a.resize(b.a.size());
for(int i=int(b.a.size())-int(x.a.size());b>=x;i--,b.clearzero()){
tmp.a.assign(b.a.begin()+i,b.a.end());
int l=,r=-,m;
while(l<=r){
m=(l+r)>>;
if(x*m<=tmp)l=m+;
else r=m-;
}
for (unsigned int j=;j<tmp.a.size();j++)b.a[j+i]=;
q.a[i]=r;tmp-=x*r;
for (unsigned int j=;j<tmp.a.size();j++)b.a[j+i]=tmp.a[j];
}
q.clearzero();return q;
}
Bignum operator%(const Bignum &x)const{return *this-*this/x*x;}
Bignum& operator/=(const Bignum &x){return *this=*this/x;}
Bignum& operator%=(const Bignum &x){return *this=*this%x;} Bignum& operator/=(long long x){
if(x>=)return *this/=Bignum(x);
else{
for (int i=int(a.size())-;i>=;i--){
int t=a[i]/x;
if(i)a[i-]+=(a[i]-t*x)*;
a[i]=t;
}
clearzero();
return *this;
}
}
Bignum operator/(long long x)const{
if(x>=)return *this / Bignum(x);
else{
Bignum y=*this;y/=x;return y;
}
}
long long operator%(long long x)const{
if(x>=)return (*this%Bignum(x)).toint();
else{
long long ans=;
for (int i=int(a.size())-;i>=;i--)ans=(ans*+a[i])%x;
return ans;
}
}
int operator%(int x)const{
return int(operator%((long long)x));
}
Bignum& operator%=(long long x){
if(x>=)return *this%=Bignum(x);
else return *this=*this%x;
}
int to2(int *s)const{
int tot=;
vector<unsigned long long>t;
for (unsigned int i=;i<a.size();i++)t.push_back((unsigned long long)a[i]);
while(!t.empty()){
for (int i=int(t.size())-;i>=;i--){
unsigned long long r=t[i]>>;
if(i==){
s[tot++]=int(t[i]&0xffffffff);
}else{
t[i-]+=(t[i]&((unsigned long long)0xffffffff))*;
}
t[i]=r;
}
while(!t.empty() && t.back()==)t.pop_back();
}
return tot;
}
bool input(){
char c;vector<int> s;a.clear();
while(((c=getchar())<'' || c>'') && c!=EOF);
if(c==EOF){a.push_back();return ;}
s.push_back(c-'');
while((c=getchar())>=''&&c<='')s.push_back(c-'');
int l=s.size();
for (int i=l-;i->=;i-=)a.push_back(s[i]+s[i-]*+s[i-]*+s[i-]*);
if(l&){
int tmp=;
for (int i=;i<(l&);i++)tmp=tmp*+s[i];
a.push_back(tmp);
}
clearzero();
return ;
}
void output()const{
for (int i=int(a.size())-;i>=;i--)
if(i==int(a.size())-)printf("%d",a[i]);
else printf("%04d",a[i]);
}
void outputln()const{
output();putchar('\n');
}
friend Bignum max(const Bignum &a,const Bignum &b){return a>b?a:b;}
friend Bignum min(const Bignum &a,const Bignum &b){return a<b?a:b;}
};
Bignum a,N;
int main()
{
int tes;gn(tes);
while(tes--){
int n;gn(n);
if(n<=){
printf("%d\n",n*(n-)*(n-)*(n-)/+n);
}else{
N=n;
a=N*(N-)*(N-)*(N-)/+N;
a.outputln();
}
}
return ;
}
高精度模板(From JCVB)的更多相关文章
- [Template]高精度模板
重新写一下高精度模板(不要问我为什么) 自认为代码风格比较漂亮(雾 如果有更好的写法欢迎赐教 封装结构体big B是压位用的进制,W是每位长度 size表示长度,d[]就是保存的数字,倒着保存,从1开 ...
- C++高精度模板
原文地址:http://blog.csdn.net/wall_f/article/details/8373395 原文只附代码,没有解析,本文增加了一些对代码的解释. 请注意:本模板不涉及实数运算与负 ...
- [note]高精度模板
高精度模板 先定义一个struct struct gj{ int l,s[N]; bool fh; void Print(){ if(fh)putchar('-'); for(int i=l;i> ...
- 高精度模板 支持各种运算 c++
绪言 自从有了高精度模板,妈妈再也不用怕我不会打高精度了! 代码 代码长度与日俱增啊~~~ #include<iostream> #include<cstring> #incl ...
- 高精度模板 Luogu P1932 A+B & A-B & A*B & A/B Problem
P1932 A+B & A-B & A*B & A/B Problem 题目背景 这个题目很新颖吧!!! 题目描述 求A.B的和差积商余! 输入输出格式 输入格式: 两个数两行 ...
- Java 大数、高精度模板
介绍: java中用于操作大数的类主要有两个,一个是BigInteger,代表大整数类用于对大整数进行操作,另一个是BigDecimal,代表高精度类,用于对比较大或精度比较高的浮点型数据进行操作.因 ...
- JAVA高精度模板
刚开始还坚持用C++写高精来着,后来发现JAVA写高精方便太多了,所以也来学习一下JAVA高精度的模板. 参考:https://www.cnblogs.com/imzscilovecode/p/883 ...
- 高精度模板 洛谷Luogu P1932 A+B & A-B & A*B & A/B Problem
P1932 A+B & A-B & A*B & A/B Problem 题目背景 这个题目很新颖吧!!! 题目描述 求A.B的和差积商余! 输入输出格式 输入格式: 两个数两行 ...
- [SinGuLaRiTy] 复习模板-高精度模板
[SinGuLaRiTy-1042] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. 结构体封装 //高精度运算 注意%I64d与%lld # ...
随机推荐
- AJPFX关于Set接口学习笔记及总结
Set接口中的方法和Collection中方法一致的.Set接口取出方式只有一种,迭代器. |--HashSet:底层数据结构是哈希表,线程是不同步的.无序,高效: HashSet集合保证元素唯一性: ...
- ssm(Spring、Springmvc、Mybatis)实战之淘淘商城-第六天(非原创)
文章大纲 一.课程介绍二.今日内容简单介绍三.Httpclient介绍与实战四.项目源码与资料下载五.参考文章 一.课程介绍 一共14天课程(1)第一天:电商行业的背景.淘淘商城的介绍.搭建项目工 ...
- poj2184 Cow Exhibition
思路: dp+滚动数组. 类似01背包. 实现: #include <iostream> #include <cstdio> #include <algorithm> ...
- 6 Specialzed layers 特殊层 第二部分 读书笔记
CAGradientLayer CAGradientLayer is used to generate a smooth gradient between two or more colors. ...
- 不能说的hidden
不能说的hidden 时光跑跑...路在脚下...晨光依在...----Vashon 1.所谓"时尚",本质上就是自己看不见自己的样子.好比我们在地球上,却感觉不到地球在动. 2. ...
- java实现排序的几种方法
package com.ywx.count; import java.util.Scanner; /** * 题目:排序的几种方式(汇总及重构) * @author Vashon(yangwenxue ...
- docker上配置nginx负载均衡
采用ubuntu系统,docker安装自行百度 1.安装tomcat docker run -d -p : tomcat docker run -d -p : tomcat 安装两个实例,端口分别为8 ...
- qt sql多重条件查询简便方法
转载请注明出处:http://www.cnblogs.com/dachen408/p/7457312.html 程序设计过程中,经常要涉及到查询,并且有很多条件,且条件可为空,如果逐个判断,会有很多情 ...
- Java JDK装配置
1- 介绍 本文章介绍JAVA开发环境安装是基于: Java8(JDK8) 2- 下载JDK http://www.oracle.com/technetwork/java/javase/dow ...
- (转)Spring简介
http://blog.csdn.net/yerenyuan_pku/article/details/52830571 阿昀以一个初学者的身份学习Spring框架,并按照一个小白的思路详细总结学习过程 ...