結果

問題 No.53 悪の漸化式
ユーザー shimomireshimomire
提出日時 2015-03-25 00:30:41
言語 C++11
(gcc 11.4.0)
結果
RE  
実行時間 -
コード長 9,465 bytes
コンパイル時間 1,034 ms
コンパイル使用メモリ 122,352 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-29 00:42:01
合計ジャッジ時間 2,957 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>// c
#include <iostream>// io
#include <iomanip>
#include <fstream>
#include <sstream>
#include <vector>// container
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <stack>
#include <algorithm>// other
#include <complex>
#include <numeric>
#include <functional>
#include <random>
#include <regex>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define ALL(c) (begin(c)),(end(c))
#define REP(i,n) FOR(i,0,n)
#define REPr(i,n) FORr(i,0,n)
#define FOR(i,l,r) for(int i=(int)(l);i<(int)(r);++i)
#define FORr(i,l,r) for(int i=(int)(r)-1;i>=(int)(l);--i)
#define EACH(it,o) for(auto it = (o).begin(); it != (o).end(); ++it)
#define IN(l,v,r) ((l)<=(v) && (v)<(r))
#define UNIQUE(v) v.erase(unique(ALL(v)),v.end())
//debug
#define DUMP(x)  cerr << #x << " = " << (x)
#define LINE()    cerr<< " (L" << __LINE__ << ")"

class range {
private:
	struct Iter{
		int v;
		int operator*(){return v;}
		bool operator!=(Iter& itr) {return v < itr.v;}
		void operator++() {++v;}
	};
	Iter i, n;
public:
	range(int n) : i({0}), n({n}) {}
	range(int i, int n) : i({i}), n({n}) {}
	Iter& begin() {return i;}
	Iter& end() {return n;}
};

//input
template<typename T1,typename T2> istream& operator >> (istream& is,pair<T1,T2>& p){is>>p.first>>p.second;return is;}
template<typename T1> istream& operator >> (istream& is,tuple<T1>& t){is >> get<0>(t);return is;}
template<typename T1,typename T2> istream& operator >> (istream& is,tuple<T1,T2>& t){is >> get<0>(t) >> get<1>(t);return is;}
template<typename T1,typename T2,typename T3> istream& operator >> (istream& is,tuple<T1,T2,T3>& t){is >>get<0>(t)>>get<1>(t)>>get<2>(t);return is;}
template<typename T1,typename T2,typename T3,typename T4> istream& operator >> (istream& is,tuple<T1,T2,T3,T4>& t){is >> get<0>(t)>>get<1>(t)>>get<2>(t)>>get<3>(t);return is;}
template<typename T1,typename T2,typename T3,typename T4,typename T5> istream& operator >> (istream& is, const tuple<T1,T2,T3,T4,T5>& t){is >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t) >> get<4>(t);return is;}
template<typename T1,typename T2,typename T3,typename T4,typename T5,typename T6> istream& operator >> (istream& is, const tuple<T1,T2,T3,T4,T5,T6>& t){is >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t) >> get<4>(t) >> get<5>(t);return is;}
template<typename T1,typename T2,typename T3,typename T4,typename T5,typename T6,typename T7> istream& operator >> (istream& is, const tuple<T1,T2,T3,T4,T5,T6,T7>& t){is >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t) >> get<4>(t) >> get<5>(t) >> get<6>(t);return is;}
template<typename T> istream& operator >> (istream& is,vector<T>& as){REP(i,as.size())is >>as[i];return is;}

//output
template<typename T> ostream& operator << (ostream& os, const set<T>& ss){for(auto a:ss){if(a!=ss.begin())os<<" "; os<<a;}return os;}
template<typename T1,typename T2> ostream& operator << (ostream& os, const pair<T1,T2>& p){os<<p.first<<" "<<p.second;return os;}
template<typename K,typename V> ostream& operator << (ostream& os, const map<K,V>& m){bool isF=true;for(auto& p:m){if(!isF)os<<endl;os<<p;isF=false;}return os;}
template<typename T1> ostream& operator << (ostream& os, const tuple<T1>& t){os << get<0>(t);return os;}
template<typename T1,typename T2> ostream& operator << (ostream& os, const tuple<T1,T2>& t){os << get<0>(t)<<" "<<get<1>(t);return os;}
template<typename T1,typename T2,typename T3> ostream& operator << (ostream& os, const tuple<T1,T2,T3>& t){os << get<0>(t)<<" "<<get<1>(t)<<" "<<get<2>(t);return os;}
template<typename T1,typename T2,typename T3,typename T4> ostream& operator << (ostream& os, const tuple<T1,T2,T3,T4>& t){os << get<0>(t)<<" "<<get<1>(t)<<" "<<get<2>(t)<<" "<<get<3>(t);return os;}
template<typename T1,typename T2,typename T3,typename T4,typename T5> ostream& operator << (ostream& os, const tuple<T1,T2,T3,T4,T5>& t){os << get<0>(t)<<" "<<get<1>(t)<<" "<<get<2>(t)<<" "<<get<3>(t)<<" "<<get<4>(t);return os;}
template<typename T1,typename T2,typename T3,typename T4,typename T5,typename T6> ostream& operator << (ostream& os, const tuple<T1,T2,T3,T4,T5,T6>& t){os << get<0>(t)<<" "<<get<1>(t)<<" "<<get<2>(t)<<" "<<get<3>(t)<<" "<<get<4>(t)<<" "<<get<5>(t);return os;}
template<typename T1,typename T2,typename T3,typename T4,typename T5,typename T6,typename T7> ostream& operator << (ostream& os, const tuple<T1,T2,T3,T4,T5,T6,T7>& t){os << get<0>(t)<<" "<<get<1>(t)<<" "<<get<2>(t)<<" "<<get<3>(t)<<" "<<get<4>(t)<<" "<<get<5>(t)<<" "<<get<6>(t);return os;}
template<typename T> ostream& operator << (ostream& os, const vector<T>& as){REP(i,as.size()){if(i!=0)os<<" "; os<<as[i];}return os;}
template<typename T> ostream& operator << (ostream& os, const vector<vector<T>>& as){REP(i,as.size()){if(i!=0)os<<endl; os<<as[i];}return os;}

//input
char tmp[1000];
#define nextInt(n) scanf("%d",&n)
#define nextLong(n) scanf("%lld",&n) //I64d
#define nextDouble(n) scanf("%lf",&n) 
#define nextChar(n) scanf("%c",&n)
#define nextString(n) scanf("%s",tmp);n=tmp

// values
template<typename T> T INF(){assert(false);};
template<> int INF<int>(){return 1<<28;};
template<> ll INF<ll>(){return 1LL<<58;};
template<> double INF<double>(){return 1e16;};
template<> long double INF<long double>(){return 1e16;};

template<class T> T EPS(){assert(false);};
template<> int EPS<int>(){return 1;};
template<> ll EPS<ll>(){return 1LL;};
template<> double EPS<double>(){return 1e-8;};
template<> long double EPS<long double>(){return 1e-8;};

template<typename T,typename U> T pmod(T v,U M){return (v%M+M)%M;}

template<typename T> class Monoid{
public:
	virtual T e()=0,c(T,T)=0;
};
template <typename T> using CommutativeMonoid = Monoid<T>;

template<typename T> class SemiRing:public virtual CommutativeMonoid<T>{
public:
	virtual T me()=0,mc(T,T)=0;
};

template<typename T> class Group:public virtual Monoid<T>{
public:
	virtual T inv(T)=0;
};
template <typename T> using CommutativeGroup = Group<T>;

template<typename T> class Ring:public virtual Group<T>,public virtual SemiRing<T>{
public:
};

template<typename T> class ZRing:public Ring<T>{
public:
	T e(){return 0;}
	T c(T a,T b){return a+b;}
	T inv(T a){return -a;}
	T me(){return 1;}
	T mc(T a,T b){return a*b;}
	static ZRing<T>& i(){static ZRing<T> i;return i;}
};

template<typename D> int sig(D a,D b=0){return a<b-EPS<D>()?-1:a>b+EPS<D>()?1:0;}

namespace Matrix{
	
	class Inconsistent{};//none
	class Ambiguous{};//many
	class NoSolution{};

	template <typename T> class Mat :public vector<vector<T>>{
		public:
		const int H,W;Ring<T>& f;
		Mat(int H,int W,Ring<T>& f) : vector<vector<T>>(H,vector<T>(W,f.e())),H(H),W(W),f(f){}

		inline Mat e(){
			return vector<vector<T>>(H,vector<T>(W,f.e()));
		}
		inline Mat c(Mat a,Mat b){return a+b;}
		inline Mat<T> inv(Mat<T> a){return -a;}
		const inline Mat me(){
			static Mat A(H,W);
			for(int y:range(H))for(int x:range(W))A[y][x]=f.e();
			for(int i:range(min(H,W)))A[i][i]=f.me();
			return A;	
		}
		inline Mat<T> mc(Mat<T> a,Mat<T> b){return a*b;}
		
		//O(n^2)
		Mat operator+(const Mat& r){
			const Mat& l=*this;
			Mat res(l.H,r.W);
			for(int y:range(l.H))for(int x:range(r.W)) res[y][x]=f.c(l[y][x],r[y][x]);
			return res;
		}

		//O(n^2)
		Mat operator-(){
			Mat res=*this; for(int y:range(res.H))for(int x:range(res.W)) res[y][x]=f.inv(res[y][x]); return res;
		}
		Mat operator-(const Mat& r){ return (*this) + (-r); }

		//O(n^3)
		Mat operator*(const Mat& r){
			const Mat& l=*this;
			Mat res(l.H,r.W);
			
			for(int y:range(l.H))for(int x:range(r.W))for(int k:range(r.H))
				res[y][x]=f.c(res[y][x],f.mc(l[y][k],r[k][x]));
			return res;
		}
		//O(n^2)
		vector<T> operator*(const vector<T>& r){
			const Mat& l=*this;
			vector<T> res(r.size());
			for(int y:range(l.H))for(int x:range(l.W))res[y]=f.c(res[y],f.mc(l[y][x],r[x]));
			return res;
		}

		// O(n^3*log(d))
		// Mat x must be n*n size
		//:TODO abstraction
		Mat pow(ll d){
			Mat x=*this,res=e();
			while(d!=0){
				if(d%2)res=res*x;
				x=x*x;
				d/=2;
			}
			return res;
		}

		// //連立方程式
		// const int gf=2;
		// T mod(T v){return mod(v,gf);}
		// T inv(T v){return invs[v];}

		//O(n^3)
		//Ax = b
		void gauss(vector<T>& b){
			Mat A=*this;
			const int H = A.H,W=A.W;
			int py =0,px = 0;
			while(py < H && px < W){
				for(int y=py+1;y<H;y++){ // pivot
					if(sig(abs(A[y][px]),abs(A[py][px]))>0){
						swap(A[y],A[py]);swap(b[y],b[py]);
					}
				}
				if(sig(A[py][px]) != 0){
					T d = 1/A[py][px];
					for(int x=0;x<W;x++) A[py][x] = f.mc(A[py][x],d);
					b[py] = f.mc(b[py],d);
					for(int y=py+1;y<H;y++){
						T k = A[y][px];
						for(int x=0;x<W;x++) A[y][x] = f.c(A[y][x],f.inv(f.mc(k,A[py][x])));
						b[y] = f.c(b[y],f.inv(f.mc(k,b[py])));
					}
					py++;
				}
				px++;
			}
			for(int y=py;y<H;y++)if(sig(b[y]) != 0)throw NoSolution();
			if(py < W || px < W)throw Ambiguous();

			for(int x=W-1;x>=0;x--)for(int y=0;y<x;y++){
				b[y] = f.c(b[y],f.inv(f.mc(b[x],A[y][x])));
			}
		}
	};
}
using namespace Matrix;


class Main{
	public:
	void run(){
		int N;cin >> N;
		Mat<double> mat(max(N+1,2),max(N+1,2),ZRing<double>::i());

		vector<double> bs(max(N+1,2));bs[0]=4;bs[1]=3;
		mat[0][0]=1;
		mat[1][1]=1;
		for(int k=2;k<=N;k++){
			mat[k][k]=-4;
			mat[k][k-1]=19;
			mat[k][k-2]=-12;
		}
		mat.gauss(bs);
		cout << bs[N]<<endl;
	}
};

int main(){
	cout <<fixed<<setprecision(20);
	cin.tie(0);
	ios::sync_with_stdio(false);
	Main().run();
	return 0;
}
0