1. 程式人生 > >牛客No.5 B-div[高精度模板和pell]

牛客No.5 B-div[高精度模板和pell]

題意:根據題意轉換能得到一個公式,是pell模型

佩爾方程:

形如x2-D*y2=1(D是一個固定的正整數且D不是完全平方數)的方程稱為佩爾方程

佩爾方程定理:

佩爾方程總有正整數解,若(x1,y1)是使x1最小的解,則每個解(xk,yk)都可以通過取冪得到: 
xk + yk * sqrt(D) = (x1 + y1 *sqrt(D))k

xn+1 = x0_xn +Dy0_yn , yn+1 = y0_xn+ x0_yn

xn+2 = 2x0_xn+1-xn   yn+2 = 2x0_yn+1-yn (NB的公式)
#include<cstdio>
#include<vector>
#include<cmath>
#include<string>
#include<queue>
#include<set>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<map>
#define PI acos(-1.0)
#define pb push_back
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e6+6;
const int MOD=998244353;
template <class T>
bool sf(T &ret){ //Faster Input
    char c; int sgn; T bit=0.1;
    if(c=getchar(),c==EOF) return 0;
    while(c!='-'&&c!='.'&&(c<'0'||c>'9')) c=getchar();
    sgn=(c=='-')?-1:1;
    ret=(c=='-')?0:(c-'0');
    while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
    if(c==' '||c=='\n'){ ret*=sgn; return 1; }
    while(c=getchar(),c>='0'&&c<='9') ret+=(c-'0')*bit,bit/=10;
    ret*=sgn;
    return 1;
}
const int mlen = 2000, base = 1e9;

struct Big {
  int a[mlen];
  Big(void) {}
  Big(int x) : a() { a[0] = x; }

  void operator += (const Big &b) {
    for (int i = 0; i < mlen; ++i) {
      a[i] += b.a[i];
      if (a[i] >= base) {
        a[i] -= base;
        ++a[i + 1];
      }
    }
  }

  void operator -= (const Big &b) {
    for (int i = 0; i < mlen; ++i) {
      a[i] -= b.a[i];
      if (a[i] < 0) {
        a[i] += base;
        --a[i + 1];
      }
    }
  }

  void operator *= (int x) {
    ll s = 0;
    for (int i = 0; i < mlen; ++i) {
      s += (ll)a[i] * x;
      a[i] = s % base;
      s /= base;
    }
  }

  bool operator < (const Big &b) const {
    for (int i = mlen - 1; i >= 0; --i) {
      if (a[i] != b.a[i]) {
        return a[i] < b.a[i];
      }
    }
    return false;
  }

  void Read(void) {
    char s[mlen];
    scanf("%s", s);
    memset(a, 0, sizeof a);
    int top = 0;
    for (int r = strlen(s); r > 0; r -= 9) {
      int l = max(0, r - 9);
      int x = 0;
      for (int i = l; i < r; ++i) {
        x = 10 * x + s[i] - '0';
      }
      a[top++] = x;
    }
  }

  void Write(void) const {
    int i;
    for (i = mlen - 1; i >= 0; --i) {
      if (a[i]) break;
    }
    printf("%d", a[i]);
    for (--i; i >= 0; --i) {
      printf("%09d", a[i]);
    }
    puts("");
  }
};
int main(void){
    vector<Big> t;
    Big m;
    m.Read();
    Big x1=0,x2=2,x3;
    /// ans=x2;
    while(x2<m){
        x3=x2;
        x2*=6;
        x2-=x1;
        x1=x3;
    }
    Big y1=0,y2=6,y3;
    while(y2<m){
        y3=y2;
        y2*=14;
        y2-=y1;
        y1=y3;
    }
    min(y2,x2).Write();
    return 0;
}