P1045 麦森数

别问我为什么要写水题

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = 505;

struct meow {
    int a[N], n;
    int& operator [](int x) {return a[x];}
    meow(int x = 0) {n = 1; memset(a, 0, sizeof(a)); a[1] = x;}
} ;

meow operator *(meow &a, meow &b) {
    const int B = 10;
    meow c;
    for(int i=1; i<=a.n; i++) {
        int g=0;
        for(int j=1; j<=b.n; j++) if(i+j-1 <= 500)
            g += c[i+j-1]+a[i]*b[j], c[i+j-1] = g%B, g/=B;
        if(i + b.n <= 500) c[i+b.n] = g;
    }
    c.n = min(500, a.n + b.n); 
    while(c.n>1 && c[c.n]==0) c.n--;
    return c;
}

meow operator ^(meow a, int b) {
    meow ans(1);
    for(; b; b >>= 1, a = a * a) 
        if(b & 1) ans = ans * a;
    return ans;
}

int n;
int main() {
    //freopen("in", "r", stdin);
    scanf("%d", &n);
    meow a(2);
    a = a ^ n; a[1]--;

    printf("%.0lf\n", floor(n * log10(2) + 1));
    for(int i=500; i > a.n; i--) putchar('0'), i % 50 == 1 ? puts("") : 1+1;
    for(int i=a.n; i>=1; i--) putchar(a[i] + '0'), i % 50 == 1 ? puts("") : 1+1;
}
posted @ 2017-04-29 16:23  Candy?  阅读(307)  评论(0编辑  收藏  举报