結果
問題 | No.526 フィボナッチ数列の第N項をMで割った余りを求める |
ユーザー | acguy |
提出日時 | 2023-11-13 13:07:49 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 9,608 bytes |
コンパイル時間 | 2,517 ms |
コンパイル使用メモリ | 213,336 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-09-26 03:16:37 |
合計ジャッジ時間 | 3,007 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 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 | 2 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 | AC | 2 ms
5,376 KB |
testcase_12 | AC | 2 ms
5,376 KB |
testcase_13 | AC | 2 ms
5,376 KB |
testcase_14 | AC | 2 ms
5,376 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; #define ll long long #define ull unsigned long long #ifdef ONLINE_JUDGE #define DPRINT(str) #else #define DPRINT(str) std::cout<<(#str)<< " = "<<(str)<<std::endl #endif #define PRINT(os, c) \ do { \ os << "[ "; \ for (auto i = c.begin(); i != c.end(); i++) { \ if (i != c.begin()) { \ os << ", "; \ } \ os << *i; \ } \ os << " ]"; \ return os; \ } while (0) #define PRINT_MAP(os, c) \ do { \ os << "{ "; \ for (auto i = c.begin(); i != c.end(); i++) { \ if (i != c.begin()) { \ os << ", "; \ } \ os << (*i).first << " => " << (*i).second; \ } \ os << " }"; \ return os; \ } while (0) #define PRINT_SET(os, c) \ do { \ os << "( "; \ for (auto i = c.begin(); i != c.end(); i++) { \ if (i != c.begin()) { \ os << ", "; \ } \ os << *i; \ } \ os << " )"; \ return os; \ } while (0) template <typename T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& c); template <typename T> std::ostream& operator<<(std::ostream& os, const std::deque<T>& c); template <typename T> std::ostream& operator<<(std::ostream& os, const std::list<T>& c); template <typename T> std::ostream& operator<<(std::ostream& os, const std::forward_list<T>& c); template <typename T, typename U> std::ostream& operator<<(std::ostream& os, const std::map<T,U>& c); template <typename T, typename U> std::ostream& operator<<(std::ostream& os, const std::multimap<T,U>& c); template <typename T, typename U> std::ostream& operator<<(std::ostream& os, const std::unordered_map<T,U>& c); template <typename T, typename U> std::ostream& operator<<(std::ostream& os, const std::unordered_multimap<T,U>& c); template <typename T> std::ostream& operator<<(std::ostream& os, const std::set<T>& c); template <typename T> std::ostream& operator<<(std::ostream& os, const std::multiset<T>& c); template <typename T> std::ostream& operator<<(std::ostream& os, const std::unordered_set<T>& c); template <typename T> std::ostream& operator<<(std::ostream& os, const std::unordered_multiset<T>& c); template <typename T, typename U> std::ostream& operator<<(std::ostream& os, const std::pair<T, U>& p); template<typename... Args> std::ostream& operator<<(std::ostream& os, const std::tuple<Args...>& t); template <typename Head, typename... Tail> std::ostream& operator<<(std::ostream& os, std::priority_queue<Head, Tail...> c); template <typename T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& c) { PRINT(os, c); } template <typename T> std::ostream& operator<<(std::ostream& os, const std::deque<T>& c) { PRINT(os, c); } template <typename T> std::ostream& operator<<(std::ostream& os, const std::list<T>& c) { PRINT(os, c); } template <typename T> std::ostream& operator<<(std::ostream& os, const std::forward_list<T>& c) { PRINT(os, c); } template <typename T, typename U> std::ostream& operator<<(std::ostream& os, const std::map<T,U>& c) { PRINT_MAP(os, c); } template <typename T, typename U> std::ostream& operator<<(std::ostream& os, const std::multimap<T,U>& c) { PRINT_MAP(os, c); } template <typename T, typename U> std::ostream& operator<<(std::ostream& os, const std::unordered_map<T,U>& c) { PRINT_MAP(os, c); } template <typename T, typename U> std::ostream& operator<<(std::ostream& os, const std::unordered_multimap<T,U>& c) { PRINT_MAP(os, c); } template <typename T> std::ostream& operator<<(std::ostream& os, const std::set<T>& c) { PRINT_SET(os, c); } template <typename T> std::ostream& operator<<(std::ostream& os, const std::multiset<T>& c) { PRINT_SET(os, c); } template <typename T> std::ostream& operator<<(std::ostream& os, const std::unordered_set<T>& c) { PRINT_SET(os, c); } template <typename T> std::ostream& operator<<(std::ostream& os, const std::unordered_multiset<T>& c) { PRINT_SET(os, c); } template <typename T, typename U> std::ostream& operator<<(std::ostream& os, const std::pair<T, U>& p) { os << "< " << p.first << ", " << p.second << " >"; return os; } template<typename Head> void PRINT_PARAMS(std::ostream& os, const Head& head){ os<<head; } template<typename Head, typename... Tail> void PRINT_PARAMS(std::ostream& os, const Head& head, const Tail&... tail){ os<<head<<", "; PRINT_PARAMS(os, tail...); } template<typename T, size_t... I> void PRINT_TUPLE_WITH_SEQUENCE( std::ostream& os, const T t, std::index_sequence<I...>){ PRINT_PARAMS(os, std::get<I>(t)...); } template<typename... Args> std::ostream& operator<<(std::ostream& os, const std::tuple<Args...>& t){ os<<"< "; PRINT_TUPLE_WITH_SEQUENCE(os, t, std::make_index_sequence<std::tuple_size<std::tuple<Args...>>::value>{}); os<<" >"; return os; } template <typename Head, typename... Tail> std::ostream& operator<<(std::ostream& os, std::priority_queue<Head, Tail...> c) { vector<Head> tmp; while(!c.empty()){ tmp.push_back(c.top()); c.pop(); } PRINT(os, tmp); } template<typename Head> void SCAN_PARAMS(std::istream& is, Head& head){ is>>head; } template<typename Head, typename... Tail> void SCAN_PARAMS(std::istream& is, Head& head, Tail&... tail){ is>>head; SCAN_PARAMS(is, tail...); } template<typename T, size_t... I> void SCAN_TUPLE_WITH_SEQUENCE( std::istream& is, T& t, std::index_sequence<I...>){ SCAN_PARAMS(is, std::get<I>(t)...); } template<typename... Args> std::istream& operator>>(std::istream& is, std::tuple<Args...>& t); template <typename T> std::istream& operator>>(std::istream& is, std::vector<T>& c); template <typename T, typename U> std::istream& operator>>(std::istream& is, std::pair<T, U>& p); template<typename... Args> std::istream& operator>>(std::istream& is, std::tuple<Args...>& t){ SCAN_TUPLE_WITH_SEQUENCE(is, t, std::make_index_sequence<std::tuple_size<std::tuple<Args...>>::valu\ e>{}); return is; } template <typename T> std::istream& operator>>(std::istream& is, std::vector<T>& c){ for(auto& e:c) is>>e; return is; } template <typename T, typename U> std::istream& operator>>(std::istream& is, std::pair<T, U>& p) { is >> p.first >> p.second; return is; } template<typename T, typename U> std::pair<T,U> operator+(const std::pair<T,U>& l, const std::pair<T,U>& r){ return {l.first+r.first, l.second+r.second}; } template<typename T, typename U> std::pair<T,U> operator-(const std::pair<T,U>& l, const std::pair<T,U>& r){ return {l.first-r.first, l.second-r.second}; } template<typename T, typename U> std::pair<T,U> operator*(const std::pair<T,U>& l, const int n){ return {l.first*n, l.second*n}; } template<typename T, typename U> std::pair<T,U> operator/(const std::pair<T,U>& l, const int n){ return {l.first/n, l.second/n}; } template<typename T> inline T lsb(const T& n){ return n & (~n + 1); } unsigned long long int factorial(unsigned long long int n){ if(n<=1)return 1; return n*factorial(n-1); } unsigned long long int combination(unsigned long long int n, unsigned long long int r, vector<vector<unsigned long long int>>& v){ if(n-r<r)r=n-r; if(v[n][r])return v[n][r]; if(r==0)return v[n][r]=1; return v[n][r]=combination(n-1,r-1,v)+combination(n-1, r, v); } unsigned long long int combination(unsigned long long int n, unsigned long long int r){ if(n<r)return 0; if(n-r<r)r=n-r; if(r==0)return 1; vector<vector<unsigned long long int>> v(n+1,vector<unsigned long long int>(r+1, 0)); return combination(n, r, v); } template<typename T> T exponentiation(T n, T m, T mod = 0){ T ret = 1; for(T i = 0;(m>>i)>0;i++){ if((1<<i)&m){ ret*=n; if(mod>0)ret%=mod; } n=n*n; if(mod>0)n%=mod; } return ret; } template<typename T> vector<vector<T>> matrix_multiply(const vector<vector<T>>& X, const vector<vector<T>>& Y, ll M) { vector<vector<T>> Z(X.size(), vector<T>(Y[0].size())); for(int i = 0;i<X.size();i++){ for(int k = 0;k<Y.size();k++){ for(int j = 0;j<Y[0].size();j++){ // Z[i][j] += X[i][k]*Y[k][j]; Z[i][j] = (Z[i][j] + X[i][k]*Y[k][j])%M; } } } return Z; } template<typename T> vector<vector<T>> matrix_pow(vector<vector<T>> A, ll n, ll M) { vector<vector<T>> B(A.size(), vector<T>(A[0].size())); for(int i = 0;i<B.size();i++){ B[i][i] = 1; } while (n>0) { if (n & 1) B = matrix_multiply(B, A, M); A = matrix_multiply(A, A, M); n = n >> 1; } return B; } int main(void){ ll N,M; cin>>N>>M; N--; vector<vector<ll>> A({{1, 1},{1,0}}); // DPRINT(A); A = matrix_pow(A, N, M); // DPRINT(A); cout<<A[1][0]<<endl; return 0; }