結果

問題 No.1595 The Final Digit
ユーザー inksamuraiinksamurai
提出日時 2021-07-09 22:12:35
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 3,322 bytes
コンパイル時間 1,194 ms
コンパイル使用メモリ 118,920 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-14 09:22:54
合計ジャッジ時間 2,159 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <tuple>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <functional>
#include <numeric>
#include <iomanip> 
#include <stdio.h>
#include <assert.h>
//eolibraries
#define lnf 3999999999999999999
#define inf 999999999
#define fi first
#define se second
#define pb push_back
#define ll long long
#define ld long double
#define all(c) (c).begin(),(c).end()
#define sz(c) (int)(c).size()
#define make_unique(a) sort(all(a)),a.erase(unique(all(a)),a.end())
#define pii pair <int,int>
#define rep(i,n) for(int i = 0 ; i < n ; i++) 
#define drep(i,n) for(int i = n-1 ; i >= 0 ; i--)
#define crep(i,x,n) for(int i = x ; i < n ; i++)
#define vi vector <int> 
#define vec(...) vector<__VA_ARGS__>
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
//eodefine
using namespace std;

const int max_n = 103002;
//snuke's modular int
template <ll mod>
struct modularint{
	ll x;
	modularint(ll x=0):x(x%mod){}
	modularint& operator+=(const modularint a){
		if ((x += a.x) >= mod) x -= mod;
		return *this;
	}
	modularint& operator-=(const modularint a){
		if ((x += mod-a.x) >= mod) x -= mod;
		return *this;
	}
	modularint& operator*=(const modularint a){
		(x *= a.x) %= mod;
		return *this;
	}
	modularint operator+(const modularint a)const{
		modularint res(*this);
		return res+=a;
	}
	modularint operator-(const modularint a)const{
		modularint res(*this);
		return res-=a;
	}
	modularint operator*(const modularint a)const{
		modularint res(*this);
		return res*=a;
	}
	modularint pow(int n)const{
		modularint res=1,x(*this);
		while(n){
			if(n&1)res*=x;
			x*=x;
			n>>=1;
		}
		return res;
	}
	modularint inv()const{
		return pow(mod-2);
	}
};
using mint = modularint<10>;

template <typename T>
struct matrix{
	vec(vec(T)) a;
	//initilize matrix here
	matrix(){}
	matrix(int h,int w){
		a.clear();
		a.resize(h,vec(T)(w));
	}
	matrix(vec(vec(T)) nea){
		a=nea;
	}
	//outer vector size here
	int size()const{ return a.size(); }
	const vector<T>& operator[](int i)const{ return a[i]; }
	vector<T>& operator[](int i){ return a[i]; }
	matrix<T>& operator *=(const matrix<T>& rhs){
		int h=a.size(), w=rhs[0].size(), c=rhs.size();
		matrix<T> res(h,w);
		rep(i,h){
			rep(j,w){
				rep(k,c){
					res[i][j] += a[i][k] * rhs[k][j];
				}
			}	
		}
		this->a = res.a;
		return *this;
	}
	matrix<T> operator *(const matrix<T>& rhs){
		return (matrix<T>(*this) *= rhs);
	}
};
template <typename T>
matrix<T> idenmat(int n){
	matrix<T> res(n,n);
	rep(i,n) res.a[i][i] = 1;
	return res;
};
template <typename T>
matrix<T> pow(matrix<T> mat,ll n){
	matrix<T> res=idenmat<T>(mat.size());
	while(n){
		if(n&1) res*=mat;
		mat*=mat;
		n>>=1;
	}
	return res;
};

int main(){
fcin;
	vec(vec(mint)) a(3,vec(mint)(3));
	a[0][0]=1;
	a[1][0]=1;
	a[2][0]=1;
	a[1][2]=1;
	a[0][1]=1;
	// a[0][2]=1;

	matrix<mint> mat(a);
	matrix<mint> now = idenmat<mint>(3);

	matrix<mint> s(vec(vec(mint))(1,vec(mint)(3)));
	
	ll p,q,r,k;
	cin>>p>>q>>r>>k;
	s[0]={r,q,p};
	mat = pow(mat,k-3);
	s *= mat;
	// rep(i,3){
	// 	cout<<s.a[0][i].x<<" ";
	// }
	cout<<s.a[0][0].x<<"\n";
/*
*/
	return 0;
}
0