結果
問題 | No.891 隣接3項間の漸化式 |
ユーザー |
|
提出日時 | 2025-01-22 19:56:48 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 6,326 bytes |
コンパイル時間 | 7,257 ms |
コンパイル使用メモリ | 333,704 KB |
実行使用メモリ | 6,820 KB |
最終ジャッジ日時 | 2025-01-22 19:56:57 |
合計ジャッジ時間 | 6,968 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 39 |
ソースコード
#include<bits/stdc++.h>#include<atcoder/all>using namespace std;namespace my{using ml=atcoder::modint1000000007;auto&operator>>(istream&i,ml&x){int t;i>>t;x=t;return i;}auto&operator<<(ostream&o,const ml&x){return o<<(int)x.val();}#define done(...) return pp(__VA_ARGS__)#define LL(...) ll __VA_ARGS__;lin(__VA_ARGS__)#define FO(n) for(ll ij=n;ij-->0;)#define FOR(i,...) for(auto[i,i##stop,i##step]=range(0,__VA_ARGS__);i<i##stop;i+=i##step)#define fo(i,...) FO##__VA_OPT__(R)(i __VA_OPT__(,__VA_ARGS__))#define fe(a,i,...) for(auto&&__VA_OPT__([)i __VA_OPT__(,__VA_ARGS__]):a)#define single_testcase void solve();}int main(){my::io();my::solve();}namespace my{void io(){cin.tie(nullptr)->sync_with_stdio(0);cout<<fixed<<setprecision(15);}using ll=long long;using ull=unsigned long long;using ulll=__uint128_t;using lll=__int128_t;istream&operator>>(istream&i,ulll&x){ull t;i>>t;x=t;return i;}ostream&operator<<(ostream&o,const ulll&x){return(x<10?o:o<<x/10)<<ll(x%10);}istream&operator>>(istream&i,lll&x){ll t;i>>t;x=t;return i;}ostream&operator<<(ostream&o,const lll&x){return o<<string(x<0,'-')<<ulll(x>0?x:-x);}constexpr auto range(bool s,auto...a){array<ll,3>r{0,0,1};ll I=0;((r[I++]=a),...);if(!s&&I==1)swap(r[0],r[1]);r[0]-=s;if(s)r[2]*=-1;return r;}constexpr char newline=10;constexpr char space=32;lll pow(lll x,ll n){assert(n>=0);lll r=1;while(n)n&1?r*=x:r,x*=x,n>>=1;return r;}template<ll k>auto pack_kth(const auto&...a){return get<k>(make_tuple(a...));}template<class T,size_t...I>auto pack_slice_impl(index_sequence<I...>, const auto&...a){return array<T,sizeof...(I)>{get<I>(forward_as_tuple(a...))...};}template<class T,size_t n>auto pack_slice(const auto&...a){return pack_slice_impl<T>(make_index_sequence<n>{},a...);}template<class V>concept vectorial=is_base_of_v<vector<typename V::value_type>,V>;template<class V>istream&operator>>(istream&i,vector<V>&v){fe(v,e)i>>e;return i;}template<class V>ostream&operator<<(ostream&o,const vector<V>&v){fe(v,e)o<<e<<string(&e!=&v.back(),vectorial<V>?newline:space);return o;}template<class V>struct vec:vector<V>{using vector<V>::vector;vec(const vector<V>&v){vector<V>::operator=(v);}template<class...A>requires(sizeof...(A)>=3)vec(A...a){const ll n=sizeof...(a)-1;auto t=pack_slice<ll,n>(a...);ll s[n];fo(i,n)s[i]=t[i];*this=make_vec(s,pack_kth<n>(a...));}template<class T,ll n,ll i=0>static auto make_vec(const ll(&s)[n],T x){if constexpr(i==n-1)return vec<T>(s[i],x);else{auto X=make_vec<T,n,i+1>(s,x);return vec<decltype(X)>(s[i],X);}}vec&operator^=(const vec&u){this->insert(this->end(),u.begin(),u.end());return*this;}vec operator^(const vec&u)const{return vec{*this}^=u;}vec&operator+=(const vec&u){vec&v=*this;fo(i,v.size())v[i]+=u[i];return v;}vec&operator-=(const vec&u){vec&v=*this;fo(i,v.size())v[i]-=u[i];return v;}vec operator+(const vec&u)const{return vec{*this}+=u;}vec operator-(const vec&u)const{return vec{*this}-=u;}vec&operator++(){fe(*this,e)++e;return*this;}vec&operator--(){fe(*this,e)--e;return*this;}vec operator-()const{vec v=*this;fe(v,e)e=-e;return v;}ll size()const{return vector<V>::size();}};template<ll rank,class T>struct tensor_helper{using type=vec<typename tensor_helper<rank-1,T>::type>;};template<class T>struct tensor_helper<0,T>{using type=T;};template<ll rank,class T>using tensor=typename tensor_helper<rank,T>::type;template<class...A>requires(sizeof...(A)>=2)vec(A...a)->vec<tensor<sizeof...(a)-2,remove_reference_t<decltype(get<sizeof...(a)-1>(declval<tuple<A...>>()))>>>;vec(ll)->vec<ll>;void lin(auto&...a){(cin>>...>>a);}template<char c=space>void pp(const auto&...a){ll n=sizeof...(a);((cout<<a<<string(--n>0,c)),...);cout<<newline;}template<class T>struct matrix:vec<vec<T>>{using vec<vec<T>>::vec;matrix()=default;matrix(ll h){this->resize(h,vec<T>(h));}matrix(ll h,ll w,T x={}){this->resize(h,vec<T>(w,x));}inline ll h()const{return this->size();}inline ll w()const{return this->size()?(*this)[0].size():0;}auto operator*(const matrix&a)const{return matrix{*this}*=a;}auto&operator*=(const matrix&a){assert(w()==a.h());matrix r(h(),a.w());fo(i,h())fo(k,w())fo(j,a.w())r[i][j]+=(*this)[i][k]*a[k][j];swap(*this,r);return*this;}friend vec<T>operator*(const vec<T>&v,const matrix&a){assert(v.size()==a.h());vec<T>r(a.w());fo(i,a.h())fo(j,a.w())r[i]+=v[i]*a[i][j];return r;}auto pow(ll n)const{matrix x{*this};matrix r(h());fo(i,h())r[i][i]=1;while(n)n&1?r*=x:r,x*=x,n>>=1;return r;}T det()const{assert(h()==w());matrix a{*this};bool is_neg=0;fo(I,h()){if(a[I][I]==0){fo(i,I+1,h()){if(a[i][I]!=0){swap(a[i],a[I]);is_neg^=1;break;}}if(a[I][I]==0)return 0;}fo(i,I+1,h()){while(a[i][I]!=0){ll q=a[I][I].val()/a[i][I].val();T minus_q{-q};fo(j,I,w())a[I][j]+=a[i][j]*minus_q;swap(a[I],a[i]);is_neg^=1;}}}T r=1;fo(I,h())r*=a[I][I];return r*(1-is_neg*2);}auto inner_gauss_jordan_elimination()const{matrix r{*this};ll I=0;T det_prime_mod=1;fo(J,w()){if(I==h())break;if(r[I][J]==0){fo(i,I+1,h()){if(r[i][J]!=0){swap(r[i],r[I]);det_prime_mod=-det_prime_mod;break;}}if(r[I][J]==0){det_prime_mod=0;continue;}}det_prime_mod*=r[I][J];T C=r[I][J].inv();fo(j,J,w())r[I][j]*=C;fo(i,h())if(T c=r[i][J];i!=I)fo(j,J,w())r[i][j]-=r[I][j]*c;++I;}return tuple{r,I,det_prime_mod};}auto gauss_jordan_elimination()const{return get<0>(inner_gauss_jordan_elimination());}auto inv()const{assert(h()==w());assert(det()!=0);matrix a(h(),w()*2);fo(i,h())fo(j,w())a[i][j]=(*this)[i][j];fo(i,h())a[i][i+w()]=1;a=a.gauss_jordan_elimination();matrix r(h(),w());fo(i,h())fo(j,w())r[i][j]=a[i][j+w()];return r;}};single_testcasevoid solve(){LL(a,b,N);if(N==0)done(0);matrix<ml>A{{a,b},{1,0}};A=A.pow(N-1);pp(A[0][0]);}}