// X ^ Y - 1 を GMPで求める
//
// gcc -lgmp -O3 でコンパイル
//   [参考]
//      http://d...content-available-to-author-only...e.jp/pyopyopyo/20090303/p1
//      http://t...content-available-to-author-only...e.jp/data/gmplibrary.txt
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <gmp.h>

//      main
int main()
{
    unsigned long int xx, yy;
    mpz_t result;
    clock_t start, end;

    // init
    mpz_init(result);

    // 入力
    fprintf(stderr, "X ^ Y - 1 を求めます。X, Y を \"X^Y\" の形式で入力してください\n :");
    scanf("%d^%d", &xx, &yy);
    if (xx < 2 || yy < 1) {
        fprintf(stderr, "入力エラー\n");
        exit(1);
    }
    // 計算
    fprintf(stderr, "計算開始 (%d ^ %d - 1 を求めます)\n", xx, yy);
    start = clock();
    mpz_ui_pow_ui(result, xx, yy);
    mpz_sub_ui(result, result, 1);
    end = clock();
    fprintf(stderr, "計算終了 (%.3fs)\n", (double) (end - start) / CLOCKS_PER_SEC);

    // 出力
#if 1
    FILE *fp = fopen("res.txt", "w");
    mpz_out_str(fp, 10, result);
    fclose(fp);
#else
    gmp_printf("%Zd", result);
    // ※ たいして時間は変わらない
#endif
    end = clock();
    fprintf(stderr, "出力終了 (%.3fs)\n", (double) (end - start) / CLOCKS_PER_SEC);

    // 終了
    mpz_clear(result);
    return 0;
}

//