結果

問題 No.681 Fractal Gravity Glue
ユーザー beetbeet
提出日時 2018-04-27 23:32:43
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,529 bytes
コンパイル時間 2,301 ms
コンパイル使用メモリ 172,964 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-10 06:52:45
合計ジャッジ時間 2,917 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using Int = long long;

const Int MOD=1e9+7; //<- alert!!!
typedef vector<Int> arr;
typedef vector<arr> mat;
inline arr mul(const mat &a,arr &b,Int mod){
  arr res(b.size(),0);
  for(Int i=0;i<(Int)b.size();i++)
    for(Int j=0;j<(Int)a[i].size();j++)
      (res[i]+=a[i][j]*b[j])%=mod;
  return res;
}
inline mat mul(const mat &a,const mat &b,Int mod){
  mat res(a.size(),arr(b[0].size(),0));
  for(Int i=0;i<(Int)a.size();i++)
    for(Int j=0;j<(Int)b[0].size();j++)
      for(Int k=0;k<(Int)b.size();k++)
	(res[i][j]+=a[i][k]*b[k][j])%=mod;
  return res;
}
inline mat mat_pow(mat a,Int n,Int mod){
  mat res(a);
  for(Int i=0;i<(Int)a.size();i++)
    for(Int j=0;j<(Int)a[i].size();j++)
      res[i][j]=(i==j);
  while(n){
    if(n&1) res=mul(a,res,mod);
    a=mul(a,a,mod);
    n>>=1;
  }
  return res;
}

arr G(Int b,Int d){
  mat m(3,arr(3,0));
  m[0][0]=d+1;m[0][1]=d;
  m[1][1]=1;m[1][2]=1;
  m[2][2]=1;
  arr v({0,1,1});
  mat p=mat_pow(m,b,MOD);
  arr r=mul(p,v,MOD);
  return r;
}

//INSERT ABOVE HERE
signed main(){
  Int n,b,d;
  cin>>n>>b>>d;
  arr r=G(b,d);
  Int ans=r[0];
  if(n<=d){
    ans+=MOD-n;
    ans%=MOD;
    cout<<ans<<endl;
    return 0;
  }
  Int x=d,y=1;
  while(x*(d+1)+d<=n) x=x*(d+1)+d,y++;
  
  while(n){
    Int k=n/(x+1);
    Int v=(G(y,d)[0]+(y+1))%MOD;
    ans+=MOD-(v*k%MOD);
    ans%=MOD;
    
    //cout<<x<<" "<<y<<":"<<n<<" "<<k<<":"<<(v*k%MOD)<<endl;
    
    n-=k*(x+1);
    x=(x-d)/(d+1);
    y--;
  }
  
  cout<<ans<<endl;
  return 0;
}
0