結果

問題 No.1559 Next Rational
ユーザー tailstails
提出日時 2021-07-04 16:29:25
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 14,384 bytes
コンパイル時間 2,485 ms
コンパイル使用メモリ 192,660 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-01 05:58:03
合計ジャッジ時間 3,319 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 3 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 3 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
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("inline")
#include<bits/stdc++.h>
using namespace std;
#define MD (1000000007U)
#define MINT_W (32U)
#define MINT_R (294967268U)
#define MINT_RR (582344008U)
#define MINT_MDNINV (2226617417U)
template<class T> struct cLtraits_identity{
  using type = T;
}
;
template<class T> using cLtraits_try_make_signed =
  typename conditional<
    is_integral<T>::value,
    make_signed<T>,
    cLtraits_identity<T>
    >::type;
template <class S, class T> struct cLtraits_common_type{
  using tS = typename cLtraits_try_make_signed<S>::type;
  using tT = typename cLtraits_try_make_signed<T>::type;
  using type = typename common_type<tS,tT>::type;
}
;
void*wmem;
char memarr[96000000];
template<class S, class T> inline auto max_L(S a, T b)
-> typename cLtraits_common_type<S,T>::type{
  return (typename cLtraits_common_type<S,T>::type) a >= (typename cLtraits_common_type<S,T>::type) b ? a : b;
}
template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){
  static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
  (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] );
  (*arr)=(T*)(*mem);
  (*mem)=((*arr)+x);
}
template<class T> inline void walloc1d(T **arr, int x1, int x2, void **mem = &wmem){
  walloc1d(arr, x2-x1, mem);
  (*arr) -= x1;
}
struct Mint{
  unsigned val;
  Mint(){
    val=0;
  }
  Mint(int a){
    val = mulR(a);
  }
  Mint(unsigned a){
    val = mulR(a);
  }
  Mint(long long a){
    val = mulR(a);
  }
  Mint(unsigned long long a){
    val = mulR(a);
  }
  inline unsigned mulR(unsigned a){
    return (unsigned long long)a*MINT_R%MD;
  }
  inline unsigned mulR(int a){
    if(a < 0){
      a = a%((int)MD)+(int)MD;
    }
    return mulR((unsigned)a);
  }
  inline unsigned mulR(unsigned long long a){
    return mulR((unsigned)(a%MD));
  }
  inline unsigned mulR(long long a){
    a %= (int)MD;
    if(a < 0){
      a += MD;
    }
    return mulR((unsigned)a);
  }
  inline unsigned reduce(unsigned T){
    unsigned m = T * MINT_MDNINV;
    unsigned t = (unsigned)((T + (unsigned long long)m*MD) >> MINT_W);
    if(t >= MD){
      t -= MD;
    }
    return t;
  }
  inline unsigned reduce(unsigned long long T){
    unsigned m = (unsigned)T * MINT_MDNINV;
    unsigned t = (unsigned)((T + (unsigned long long)m*MD) >> MINT_W);
    if(t >= MD){
      t -= MD;
    }
    return t;
  }
  inline unsigned get(){
    return reduce(val);
  }
  inline Mint &operator++(){
    (*this) += 1;
    return *this;
  }
  inline Mint &operator--(){
    (*this) -= 1;
    return *this;
  }
  inline Mint operator++(int a){
    Mint res(*this);
    (*this) += 1;
    return res;
  }
  inline Mint operator--(int a){
    Mint res(*this);
    (*this) -= 1;
    return res;
  }
  inline Mint &operator+=(Mint a){
    val += a.val;
    if(val >= MD){
      val -= MD;
    }
    return *this;
  }
  inline Mint &operator-=(Mint a){
    if(val < a.val){
      val = val + MD - a.val;
    }
    else{
      val -= a.val;
    }
    return *this;
  }
  inline Mint &operator*=(Mint a){
    val = reduce((unsigned long long)val*a.val);
    return *this;
  }
  inline Mint &operator/=(Mint a){
    return *this *= a.inverse();
  }
  inline Mint operator+(Mint a){
    return Mint(*this)+=a;
  }
  inline Mint operator-(Mint a){
    return Mint(*this)-=a;
  }
  inline Mint operator*(Mint a){
    return Mint(*this)*=a;
  }
  inline Mint operator/(Mint a){
    return Mint(*this)/=a;
  }
  inline Mint operator+(int a){
    return Mint(*this)+=Mint(a);
  }
  inline Mint operator-(int a){
    return Mint(*this)-=Mint(a);
  }
  inline Mint operator*(int a){
    return Mint(*this)*=Mint(a);
  }
  inline Mint operator/(int a){
    return Mint(*this)/=Mint(a);
  }
  inline Mint operator+(long long a){
    return Mint(*this)+=Mint(a);
  }
  inline Mint operator-(long long a){
    return Mint(*this)-=Mint(a);
  }
  inline Mint operator*(long long a){
    return Mint(*this)*=Mint(a);
  }
  inline Mint operator/(long long a){
    return Mint(*this)/=Mint(a);
  }
  inline Mint operator-(void){
    Mint res;
    if(val){
      res.val=MD-val;
    }
    else{
      res.val=0;
    }
    return res;
  }
  inline operator bool(void){
    return val!=0;
  }
  inline operator int(void){
    return get();
  }
  inline operator long long(void){
    return get();
  }
  inline Mint inverse(){
    int a = val;
    int b = MD;
    int u = 1;
    int v = 0;
    int t;
    Mint res;
    while(b){
      t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    if(u < 0){
      u += MD;
    }
    res.val = (unsigned long long)u*MINT_RR % MD;
    return res;
  }
  inline Mint pw(unsigned long long b){
    Mint a(*this);
    Mint res;
    res.val = MINT_R;
    while(b){
      if(b&1){
        res *= a;
      }
      b >>= 1;
      a *= a;
    }
    return res;
  }
  inline bool operator==(int a){
    return mulR(a)==val;
  }
  inline bool operator!=(int a){
    return mulR(a)!=val;
  }
}
;
inline Mint operator+(int a, Mint b){
  return Mint(a)+=b;
}
inline Mint operator-(int a, Mint b){
  return Mint(a)-=b;
}
inline Mint operator*(int a, Mint b){
  return Mint(a)*=b;
}
inline Mint operator/(int a, Mint b){
  return Mint(a)/=b;
}
inline Mint operator+(long long a, Mint b){
  return Mint(a)+=b;
}
inline Mint operator-(long long a, Mint b){
  return Mint(a)-=b;
}
inline Mint operator*(long long a, Mint b){
  return Mint(a)*=b;
}
inline Mint operator/(long long a, Mint b){
  return Mint(a)/=b;
}
inline int my_getchar_unlocked(){
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if(s == e && e == 1048576){
    e = fread_unlocked(buf, 1, 1048576, stdin);
    s = 0;
  }
  if(s == e){
    return EOF;
  }
  return buf[s++];
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline void rd(long long &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline void rd(Mint &x){
  int i;
  rd(i);
  x=i;
}
struct MY_WRITER{
  char buf[1048576];
  int s;
  int e;
  MY_WRITER(){
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER(){
    if(s){
      fwrite_unlocked(buf, 1, s, stdout);
    }
  }
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
  if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
    fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
  my_putchar_unlocked(a);
}
inline void wt_L(int x){
  int s=0;
  int m=0;
  char f[10];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
inline void wt_L(unsigned x){
  int s=0;
  char f[10];
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
inline void wt_L(long long x){
  int s=0;
  int m=0;
  char f[20];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
inline void wt_L(unsigned long long x){
  int s=0;
  char f[21];
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
inline void wt_L(Mint x){
  int i;
  i = (int)x;
  wt_L(i);
}
int WRITER_DOUBLE_DIGIT = 15;
inline int writerDigit_double(){
  return WRITER_DOUBLE_DIGIT;
}
inline void writerDigit_double(int d){
  WRITER_DOUBLE_DIGIT = d;
}
inline void wt_L(double x){
  const int d = WRITER_DOUBLE_DIGIT;
  int k;
  int r;
  double v;
  if(x!=x || (x==x+1 && x==2*x)){
    my_putchar_unlocked('E');
    my_putchar_unlocked('r');
    my_putchar_unlocked('r');
    return;
  }
  if(x < 0){
    my_putchar_unlocked('-');
    x = -x;
  }
  x += 0.5 * pow(0.1, d);
  r = 0;
  v = 1;
  while(x >= 10*v){
    v *= 10;
    r++;
  }
  while(r >= 0){
    r--;
    k = floor(x / v);
    if(k >= 10){
      k = 9;
    }
    if(k <= -1){
      k = 0;
    }
    x -= k * v;
    v *= 0.1;
    my_putchar_unlocked(k + '0');
  }
  if(d > 0){
    my_putchar_unlocked('.');
    v = 1;
    for(r=(0);r<(d);r++){
      v *= 0.1;
      k = floor(x / v);
      if(k >= 10){
        k = 9;
      }
      if(k <= -1){
        k = 0;
      }
      x -= k * v;
      my_putchar_unlocked(k + '0');
    }
  }
}
inline void wt_L(const char c[]){
  int i=0;
  for(i=0;c[i]!='\0';i++){
    my_putchar_unlocked(c[i]);
  }
}
inline void wt_L(string &x){
  int i=0;
  for(i=0;x[i]!='\0';i++){
    my_putchar_unlocked(x[i]);
  }
}
template<class T, class P, class M> T PowMod(T a, P b, M m){
  T r;
  r = 1;
  while(b > 0){
    if(b % 2){
      r = r * a % m;
    }
    b /= 2;
    if(b > 0){
      a = a * a % m;
    }
  }
  return r;
}
template<class S, class T> inline S chmax(S &a, T b){
  if(a<b){
    a=b;
  }
  return a;
}
template<class T> struct Polynomial{
  int d;
  int mem;
  T*c;
  Polynomial(){
    mem = 1;
    c = new T[mem];
    d = 0;
    c[0] = 0;
  }
  Polynomial(T a){
    mem = 1;
    c = new T[mem];
    d = 0;
    c[0] = a;
  }
  Polynomial(const Polynomial<T> &a){
    int i;
    d = a.d;
    mem = d + 1;
    c = new T[mem];
    for(i=(0);i<(d+1);i++){
      c[i] = a.c[i];
    }
  }
  ~Polynomial(){
    delete [] c;
  }
  void expand(int z){
    int i;
    T*cc;
    if(z <= mem){
      return;
    }
    mem =max_L(z, 2 * mem);
    cc = new T[mem];
    for(i=(0);i<(d+1);i++){
      cc[i] = c[i];
    }
    delete [] c;
    c = cc;
  }
  inline void change(const int dg, const T cf){
    expand(dg+1);
    while(d < dg){
      c[++d] = 0;
    }
    c[dg] = cf;
    while(d && c[d]==0){
      d--;
    }
  }
  inline int deg(void){
    return d;
  }
  inline T coef(const int k){
    if(k > d){
      return 0;
    }
    return c[k];
  }
  Polynomial<T>& operator=(const T a){
    d = 0;
    expand(d + 1);
    c[0] = a;
    return *this;
  }
  Polynomial<T>& operator=(const Polynomial<T> &a){
    int i;
    d = a.d;
    expand(d + 1);
    for(i=(0);i<(d+1);i++){
      c[i] = a.c[i];
    }
    return *this;
  }
  Polynomial<T>& operator+=(const Polynomial<T> &a){
    int i;
    int k;
    k =max_L(d, a.d);
    expand(k+1);
    while(d < k){
      c[++d] = 0;
    }
    for(i=(0);i<(a.d+1);i++){
      c[i] += a.c[i];
    }
    while(d && c[d]==0){
      d--;
    }
    return *this;
  }
  Polynomial<T> operator+(const Polynomial<T> &a){
    return Polynomial<T>(*this) += a;
  }
  Polynomial<T>& operator-=(const Polynomial<T> &a){
    int i;
    int k;
    k =max_L(d, a.d);
    expand(k+1);
    while(d < k){
      c[++d] = 0;
    }
    for(i=(0);i<(a.d+1);i++){
      c[i] -= a.c[i];
    }
    while(d && c[d]==0){
      d--;
    }
    return *this;
  }
  Polynomial<T> operator-(const Polynomial<T> &a){
    return Polynomial<T>(*this) -= a;
  }
  Polynomial<T>& operator*=(const Polynomial<T> &a){
    int i;
    int j;
    int k;
    T*cc;
    void*mem = wmem;
    k = d + a.d;
    expand(k+1);
    walloc1d(&cc, k+1, &mem);
    for(i=(0);i<(k+1);i++){
      cc[i] = 0;
    }
    for(i=(0);i<(d+1);i++){
      for(j=(0);j<(a.d+1);j++){
        cc[i+j] += c[i] * a.c[j];
      }
    }
    for(i=(0);i<(k+1);i++){
      c[i] = cc[i];
    }
    d = k;
    while(d && c[d]==0){
      d--;
    }
    return *this;
  }
  Polynomial<T> operator*(const Polynomial<T> &a){
    return Polynomial<T>(*this) *= a;
  }
  Polynomial<T>& operator/=(const Polynomial<T> &a){
    int i;
    int j;
    int k;
    T*cc;
    T e;
    void*mem = wmem;
    walloc1d(&cc, d-a.d, &mem);
    for(i=d; i>=a.d; i--){
      cc[i-a.d] = e = c[i] / a.c[a.d];
      for(j=(0);j<(a.d+1);j++){
        c[i-j] -= e * a.c[a.d-j];
      }
    }
    d -= a.d;
    for(i=(0);i<(d+1);i++){
      c[i] = cc[i];
    }
    return *this;
  }
  Polynomial<T> operator/(const Polynomial<T> &a){
    return Polynomial<T>(*this) /= a;
  }
  Polynomial<T>& operator%=(const Polynomial<T> &a){
    int i;
    int j;
    int k;
    T*cc;
    T e;
    void*mem = wmem;
    walloc1d(&cc, d-a.d, &mem);
    for(i=d; i>=a.d; i--){
      cc[i-a.d] = e = c[i] / a.c[a.d];
      for(j=(0);j<(a.d+1);j++){
        c[i-j] -= e * a.c[a.d-j];
      }
    }
    while(d && c[d]==0){
      d--;
    }
    return *this;
  }
  Polynomial<T> operator%(const Polynomial<T> &a){
    return Polynomial<T>(*this) %= a;
  }
  Polynomial<T>& operator*=(const T &a){
    int i;
    for(i=(0);i<(d+1);i++){
      c[i] *= a;
    }
    while(d && c[d]==0){
      d--;
    }
    return *this;
  }
  Polynomial<T> operator*(const T &a){
    return Polynomial<T>(*this) *= a;
  }
  Polynomial<T>& operator/=(const T &a){
    int i;
    for(i=(0);i<(d+1);i++){
      c[i] /= a;
    }
    while(d && c[d]==0){
      d--;
    }
    return *this;
  }
  Polynomial<T> operator/(const T &a){
    return Polynomial<T>(*this) /= a;
  }
  inline T operator()(const T x){
    int i;
    T res;
    res = 0;
    for(i=d;i>=0;i--){
      res = res * x + c[i];
    }
    return res;
  }
}
;
template<class T> Polynomial<T> operator*(const T a, const Polynomial<T> &b){
  return Polynomial<T>(b)*=a;
}
int main(){
  wmem = memarr;
  long long n;
  rd(n);
  Mint a;
  rd(a);
  Mint b;
  rd(b);
  Mint k;
  rd(k);
  Polynomial<Mint> p;
  Polynomial<Mint> m;
  Polynomial<Mint> r;
  p.change(1,1);
  m.change(2,1);
  m.change(1,-(a*a+b*b+k)/(a*b));
  m.change(0,1);
  r=PowMod(p,n-1,m);
  wt_L(r.coef(0)*a+r.coef(1)*b);
  wt_L('\n');
  return 0;
}
// cLay version 20210703-1

// --- original code ---
// //yukicoder@clay
// {
// 	ll@n;Mint@a,@b,@k;
// 	Polynomial<Mint> p,m,r;
// 	p.change(1,1);
// 	m.change(2,1);
// 	m.change(1,-(a*a+b*b+k)/(a*b));
// 	m.change(0,1);
// 	r=PowMod(p,n-1,m);
// 	wt(r.coef(0)*a+r.coef(1)*b);
// }
// 
0