zoukankan      html  css  js  c++  java
  • 紫书 例题 10-23 UVa 10213(欧拉公式+高精度)

    用欧拉公式V-E+F=2

    V是顶点数,E是边数,F是面数

    具体推导见https://blog.csdn.net/QWsin/article/details/53635397

    要用高精度

    #include<cstdio>
    #include<algorithm>
    #include<iostream>
    #include<string>
    #include<cstring>
    #include<sstream>
    #define REP(i, a, b) for(int i = (a); i < (b); i++)
    using namespace std;
    
    const int MAXN = 112;
    struct bign
    {
    	int len, d[MAXN];
    	
    	void clean() 	{ while(len > 1 && !d[len-1]) len--; }   //去零 
    	bign() 			{ memset(d, 0, sizeof(d)); len = 1; }
    	bign(int num) 	{ *this = num; }                         //bign a = 123(int) 
    	bign(char* num) { *this = num; }                         //bign a = "123"
    	bign operator = (const char* num)                        //a = "123"
    	{
    		memset(d, 0, sizeof(d));
    		len = strlen(num);
    		REP(i, 0, len) d[i] = num[len-i-1] - '0';
    		clean();
    		return *this;
    	}
    	bign operator = (int num)                               //a = 123(int) 
    	{
    		char s[20];
    		sprintf(s, "%d", num);
    		*this = s;
    		return *this;
    	}
    	
    	bign operator + (const bign& b)                        //加法 
    	{
    		bign c = *this;	int i;
    		for(i = 0; i < b.len; i++)
    		{
    			c.d[i] += b.d[i];
    			if(c.d[i] > 9) c.d[i] %= 10, c.d[i+1]++; 
    		}
    		while(c.d[i] > 9) c.d[i] %= 10, c.d[i+1]++; 
    		c.len = max(len, b.len) + 1;
    		c.clean();
    		return c;
    	}
    	bign operator - (const bign& b)                       //减法 
    	{
    		bign c = *this; int i;
    		for(i = 0; i < b.len; i++)
    		{
    			c.d[i] -= b.d[i];
    			if(c.d[i] < 0) c.d[i] += 10, c.d[i+1]--; 
    		}
    		while(c.d[i] < 0) c.d[i++] += 10, c.d[i]--; 
    		c.clean();
    		return c;
    	}
    	bign operator * (const bign& b) const              //乘法,记得这里有const 
    	{
    		bign c; c.len = len + b.len;
    		REP(i, 0, len)
    			REP(j, 0, b.len)
    				c.d[i+j] += d[i] * b.d[j];
    		REP(i, 0, c.len-1)
    			c.d[i+1] += c.d[i] / 10, c.d[i] %= 10;
    		c.clean();
    		return c;
    	}
    	bign operator / (const bign& b)                    //除法 
    	{
    		bign c = *this, a = 0;
    		int i, j;
    		for(i = len - 1; i >= 0; i--)
    		{
    			a = a * 10 + d[i];
    			for(j = 0; j < 10; j++) if(a < b*(j+1)) break;
    			c.d[i] = j;
    			a = a - b * j; 
    		}
    		c.clean();
    		return c;
    	}	
    	bign operator % (const bign& b)                    //模 
    	{
    		bign a = 0;
    		int i, j;
    		for(i = len - 1; i >= 0; i--)
    		{
    			a = a * 10 + d[i];
    			for(j = 0; j < 10; j++) if(a < b*(j+1)) break;
    			a = a - b * j; 
    		}
    		return a;
    	}
    	bign operator += (const bign& b)                 //+= 
    	{
    		*this = *this + b;
    		return *this; 
    	}
    	
    	bool operator <(const bign& b) const
    	{
    		if(len != b.len) return len < b.len;
    		for(int i = len - 1; i >= 0; i--)
    			if(d[i] != b.d[i])
    				return d[i] < b.d[i];
    		return false;
    	}
    	bool operator >(const bign& b)  const {return b < *this;}
    	bool operator <=(const bign& b) const {return !(b < *this);}
    	bool operator >=(const bign& b) const {return !(*this < b);}
    	bool operator !=(const bign& b) const {b < *this || *this < b;}
    	bool operator ==(const bign& b) const {return !(b < *this || *this < b);}
    };
    
    istream&  operator >> (istream& in, bign &x)
    {
    	int a;
        in>>a;
    	x=a;
    	return in;    
    }
    ostream&  operator << (ostream& out,const bign &x)
    {
    	for(int i = x.len - 1; i >= 0; i--)
    		printf("%d", x.d[i]);
        return out;
    }
    
    int main()
    {
    	int T;
    	scanf("%d", &T);
    	while(T--)
    	{
    		bign n;
    		cin >> n; 
    		n = n * n * n * n + (bign)23 * n * n - bign(6) * n * n * n - (bign)18 * n;
    		cout << n / 24 + (bign)1 << endl; 
    	}
    	return 0;
    }
  • 相关阅读:
    【BZOJ2741】L-分块+可持久化trie
    【BZOJ4241】历史研究-回滚莫队
    【BZOJ4137】火星商店问题(FJOI2015)-线段树分治+可持久化trie
    【HDU4117】GRE Words-AC自动机+线段树优化DP
    noi.ac系列NOIP2018模拟赛参赛实录
    【BZOJ1835】基站选址(ZJOI2010)-DP+线段树优化
    【BZOJ4912】天才黑客(SDOI2017)-最短路+虚树+线段树优化建图
    【HDU4897】Little Devil I-树链剖分
    【UOJ#282】长度测量鸡-数学证明
    10.12
  • 原文地址:https://www.cnblogs.com/sugewud/p/9819495.html
Copyright © 2011-2022 走看看