結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー namakoiscatnamakoiscat
提出日時 2022-08-02 15:35:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 4,803 bytes
コンパイル時間 2,473 ms
コンパイル使用メモリ 213,320 KB
実行使用メモリ 9,804 KB
最終ジャッジ日時 2023-09-30 15:05:16
合計ジャッジ時間 3,315 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
9,552 KB
testcase_01 AC 4 ms
9,728 KB
testcase_02 AC 4 ms
9,576 KB
testcase_03 AC 4 ms
9,600 KB
testcase_04 AC 5 ms
9,528 KB
testcase_05 AC 4 ms
9,524 KB
testcase_06 AC 5 ms
9,728 KB
testcase_07 AC 4 ms
9,596 KB
testcase_08 AC 4 ms
9,608 KB
testcase_09 AC 5 ms
9,516 KB
testcase_10 AC 5 ms
9,804 KB
testcase_11 AC 4 ms
9,728 KB
testcase_12 AC 4 ms
9,740 KB
testcase_13 AC 5 ms
9,664 KB
testcase_14 AC 5 ms
9,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// __builtin_popcount() ;
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef string st ;
typedef long double ld ;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl ;
typedef tuple<int,int,int>  ti ;
typedef tuple<ll,ll,ll> tl ;
typedef vector<vector<int>> vi ;
typedef vector<vector<ll>> vl ;
typedef vector<vector<bool>> vb ;
typedef vector<vector<st>> vs ;
typedef vector<vector<char>> vc;
const ll mod0 = 1000000007;
const ll mod1 = 998244353 ;
const ll LINF =  1000000000000000000 ;  //(10^18)
const int INF = 1000000000 ;            // (10^9)
 
 
#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front
#define all(x)            x.begin(), x.end()
#define rep(i,a,n)        for (ll i = a; i <= (n); ++i)
#define re                return 0;
#define fore(i,a)         for(auto &i:a)
#define V                 vector
 
 
ll gcd(ll a, ll b){
 
   if(b == 0){
    return a;
}
return gcd(b,a%b) ;
}
 
 
ll lcm(ll a, ll b){
    
    ll ans = a*b /gcd(a,b) ;
    return ans ;
}
 
bool nis(ll q){
    
     // 素数だったらtrue ,素数じゃなかったらfalse
     
       if(q == 1){
           
           return false ;
       }
       
      for(ll i=2;i*i <=q;i++){
          if(q%i == 0){
              return false ;
          }
      }
    return true ;
}

 ll jun(ll a,ll b, ll c,ll rank ){
     
    vector<ll> ANS ;
    ANS.pb(-LINF) ;
    ANS.pb(a) ;
    ANS.pb(b) ;
    ANS.pb(c) ;
    
    sort(all(ANS)) ;
    return ANS[rank] ;
}

#define  C            cout   
#define  E            "\n";

vector<int> par;
class UnionFind {
public:
 
  
   
  // サイズをGET!
  void init(int sz) {
       par.resize(sz,-1);
}
   // 各連結成分の一番上を返す
  int root(int x) {
    if (par[x] < 0) return x;
    return par[x] = root(par[x]);
  }
   
  // 結合作業
  bool unite(int x, int y) {
    x = root(x); y = root(y);
    if (x == y) return false;
    if (par[x] > par[y]) swap(x,y);
    par[x] += par[y];
    par[y] = x;
    return true;
  }
  // 同じグループか判定
  bool same(int x, int y) { return root(x) == root(y);}
  // グループのサイズをGET!
  int size(int x) { return -par[root(x)];}
};
 
UnionFind UF ;
 
 
 
 st Y = "Yes" ; 
 st YY = "No" ;
 
 
vector<ll> G[1 << 18] ;

void chmin(ll& x ,ll y){x = min(x,y) ;}
void chmax(ll& x ,ll y){x = max(x,y) ;}


vector<ll> Y4 = {0,1,0,-1} ;
vector<ll> X4 = {1,0,-1,0} ;

vector<ll> Y8 = {0,1,1,1,0,-1,-1,-1} ;
vector<ll> X8 = {1,1,0,-1,-1,-1,0,1} ;


vector<ll> enumdiv(ll n) { 
    vector<ll> S;
    for (ll i = 1; i*i <= n; i++) if (n%i == 0) { S.pb(i); if (i*i != n) S.pb(n / i); }
    sort(S.begin(), S.end());
    return S;
}
 
template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
template<typename T> using max_priority_queue = priority_queue<T, vector<T>, less<T>> ;
// 使用例 min_priority_queue<ll (ここは型)> Q ;


ll ppow(ll a , ll b ){
    
    if(b == 0){
        return 1 ;
    }
    
    ll ans = 1 ;
    rep(i,1,b){
        ans *= a ;
        
    }
    return ans ;
}


st KU = " " ;


V<ll> prime_broken(ll a){
    
    V<ll> ans ;
    for(ll i = 2 ;i*i<=a;i++){
        if(a %i == 0){
          while(a %i == 0){
                a /= i ;
                ans.pb(i) ;
          }
            
        }
        
    }
    
    if(a != 1)ans.pb(a) ;
    return ans ;
}


const ld pai = acos(-1) ;
using P = pair<ll,ll> ;
using Edeg = tuple<ll,ll,ll> ;


 st zz = "abcdefghijklmnopqrstuvwxyz" ;
 st ZZ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;

typedef V<ll> vec ;
typedef V<vec> mat ;
ll M ;

mat mul(mat &A ,mat&B){
    mat CC(A.size(),vec(B[0].size())) ;
    for(ll i = 0 ;i < A.size() ;i++){
        for(ll k = 0 ; k < B.size() ;k++){
            for(ll j = 0; j < B[0].size() ;j++){
                CC[i][j] = (CC[i][j] + A[i][k] * B[k][j])  % M;
            }
        }
    }
  return CC ;
}

mat POW(mat A , ll n){
    mat B(A.size(),vec(A.size())) ;
    for(ll i = 0;i< A.size() ;i++){
        B[i][i] = 1 ;
    }
    while(n > 0){
        if(n & 1) B = mul(B,A) ;
        A = mul(A,A) ;
        n >>= 1 ;
    }
    return B ;
}

int main(void){ 
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
// !!入力を受け取る!!
// !!入力を受け取る!!
// !!入力を受け取る!!
//  UF.initはいっかいだけならいいけど、二回目以降はrepで初期化

ll N;
cin>>N>>M ;

mat A(2,V<ll>(2)) ;

A[0][0] = 1 ;
A[0][1] = 1 ;
A[1][0] = 1 ;
A[1][1] = 0 ;

A = POW(A,N) ;

C << A[1][1] %M<< E
 // if(dx < 0 || dy < 0 || dx >= W || dy >= H) continue ;
 // ld p  = sqrt(abs((A[i] - A[j])*(A[i] - A[j])) + abs((B[i] - B[j])*(B[i] - B[j]))) ;
 // C << fixed << setprecision(10) << 
re
}
0