結果

問題 No.747 循環小数N桁目 Hard
ユーザー xuzijian629xuzijian629
提出日時 2018-10-26 21:17:43
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 30 ms / 2,000 ms
コード長 7,606 bytes
コンパイル時間 1,898 ms
コンパイル使用メモリ 165,576 KB
実行使用メモリ 19,972 KB
最終ジャッジ日時 2023-08-12 07:37:55
合計ジャッジ時間 8,825 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 25 ms
19,428 KB
testcase_01 AC 25 ms
19,564 KB
testcase_02 AC 25 ms
19,492 KB
testcase_03 AC 18 ms
19,484 KB
testcase_04 AC 24 ms
19,560 KB
testcase_05 AC 25 ms
19,492 KB
testcase_06 AC 25 ms
19,372 KB
testcase_07 AC 24 ms
19,424 KB
testcase_08 AC 25 ms
19,504 KB
testcase_09 AC 25 ms
19,412 KB
testcase_10 AC 24 ms
19,368 KB
testcase_11 AC 25 ms
19,404 KB
testcase_12 AC 25 ms
19,736 KB
testcase_13 AC 24 ms
19,420 KB
testcase_14 AC 25 ms
19,348 KB
testcase_15 AC 25 ms
19,556 KB
testcase_16 AC 25 ms
19,484 KB
testcase_17 AC 24 ms
19,500 KB
testcase_18 AC 24 ms
19,352 KB
testcase_19 AC 25 ms
19,424 KB
testcase_20 AC 17 ms
19,356 KB
testcase_21 AC 16 ms
19,568 KB
testcase_22 AC 16 ms
19,580 KB
testcase_23 AC 25 ms
19,404 KB
testcase_24 AC 24 ms
19,560 KB
testcase_25 AC 25 ms
19,560 KB
testcase_26 AC 16 ms
19,568 KB
testcase_27 AC 18 ms
19,424 KB
testcase_28 AC 25 ms
19,556 KB
testcase_29 AC 24 ms
19,484 KB
testcase_30 AC 25 ms
19,424 KB
testcase_31 AC 25 ms
19,404 KB
testcase_32 AC 25 ms
19,420 KB
testcase_33 AC 27 ms
19,628 KB
testcase_34 AC 25 ms
19,528 KB
testcase_35 AC 26 ms
19,492 KB
testcase_36 AC 26 ms
19,532 KB
testcase_37 AC 25 ms
19,760 KB
testcase_38 AC 19 ms
19,644 KB
testcase_39 AC 25 ms
19,560 KB
testcase_40 AC 27 ms
19,692 KB
testcase_41 AC 27 ms
19,720 KB
testcase_42 AC 26 ms
19,608 KB
testcase_43 AC 26 ms
19,564 KB
testcase_44 AC 26 ms
19,652 KB
testcase_45 AC 27 ms
19,704 KB
testcase_46 AC 27 ms
19,540 KB
testcase_47 AC 28 ms
19,620 KB
testcase_48 AC 27 ms
19,800 KB
testcase_49 AC 27 ms
19,672 KB
testcase_50 AC 26 ms
19,572 KB
testcase_51 AC 26 ms
19,636 KB
testcase_52 AC 26 ms
19,524 KB
testcase_53 AC 29 ms
19,708 KB
testcase_54 AC 27 ms
19,720 KB
testcase_55 AC 21 ms
19,804 KB
testcase_56 AC 27 ms
19,704 KB
testcase_57 AC 26 ms
19,528 KB
testcase_58 AC 30 ms
19,708 KB
testcase_59 AC 27 ms
19,648 KB
testcase_60 AC 29 ms
19,804 KB
testcase_61 AC 28 ms
19,696 KB
testcase_62 AC 28 ms
19,728 KB
testcase_63 AC 27 ms
19,632 KB
testcase_64 AC 18 ms
19,640 KB
testcase_65 AC 25 ms
19,492 KB
testcase_66 AC 26 ms
19,664 KB
testcase_67 AC 18 ms
19,832 KB
testcase_68 AC 18 ms
19,696 KB
testcase_69 AC 27 ms
19,652 KB
testcase_70 AC 26 ms
19,532 KB
testcase_71 AC 26 ms
19,612 KB
testcase_72 AC 26 ms
19,692 KB
testcase_73 AC 19 ms
19,564 KB
testcase_74 AC 28 ms
19,668 KB
testcase_75 AC 19 ms
19,532 KB
testcase_76 AC 27 ms
19,572 KB
testcase_77 AC 26 ms
19,516 KB
testcase_78 AC 27 ms
19,616 KB
testcase_79 AC 26 ms
19,684 KB
testcase_80 AC 27 ms
19,824 KB
testcase_81 AC 25 ms
19,536 KB
testcase_82 AC 26 ms
19,544 KB
testcase_83 AC 26 ms
19,784 KB
testcase_84 AC 27 ms
19,624 KB
testcase_85 AC 28 ms
19,632 KB
testcase_86 AC 27 ms
19,836 KB
testcase_87 AC 28 ms
19,708 KB
testcase_88 AC 26 ms
19,556 KB
testcase_89 AC 25 ms
19,544 KB
testcase_90 AC 25 ms
19,544 KB
testcase_91 AC 26 ms
19,560 KB
testcase_92 AC 21 ms
19,660 KB
testcase_93 AC 27 ms
19,584 KB
testcase_94 AC 22 ms
19,740 KB
testcase_95 AC 21 ms
19,800 KB
testcase_96 AC 27 ms
19,664 KB
testcase_97 AC 28 ms
19,936 KB
testcase_98 AC 29 ms
19,972 KB
testcase_99 AC 28 ms
19,740 KB
testcase_100 AC 28 ms
19,684 KB
testcase_101 AC 27 ms
19,732 KB
testcase_102 AC 26 ms
19,820 KB
testcase_103 AC 27 ms
19,604 KB
testcase_104 AC 27 ms
19,856 KB
testcase_105 AC 27 ms
19,644 KB
testcase_106 AC 27 ms
19,680 KB
testcase_107 AC 28 ms
19,600 KB
testcase_108 AC 28 ms
19,724 KB
testcase_109 AC 21 ms
19,592 KB
testcase_110 AC 28 ms
19,732 KB
testcase_111 AC 28 ms
19,588 KB
testcase_112 AC 28 ms
19,608 KB
testcase_113 AC 28 ms
19,660 KB
testcase_114 AC 28 ms
19,636 KB
testcase_115 AC 28 ms
19,612 KB
testcase_116 AC 28 ms
19,856 KB
testcase_117 AC 28 ms
19,596 KB
testcase_118 AC 28 ms
19,604 KB
testcase_119 AC 21 ms
19,644 KB
testcase_120 AC 28 ms
19,600 KB
testcase_121 AC 28 ms
19,688 KB
testcase_122 AC 27 ms
19,592 KB
testcase_123 AC 28 ms
19,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

using namespace std;
using i64 = int64_t;
using vi = vector<i64>;
using vvi = vector<vi>;
constexpr i64 MOD = 1e9 + 7;
constexpr i64 Z = i64(0);
using ii = pair<int, int>;

// 負の数は扱っていない
// 宣言するときはconvert関数を使う
// 掛け算でFFTをやるので畳み込み後の配列の最大要素を1e13程度にして誤差を小さくしたい
// BASELOGを大きくすると桁数が1/BASELOGになる代わりに配列の要素が指数関数的に大きくなる
// 掛け算をしないのであれば定数倍早くなるのでBASELOGを大きくするとよい。掛け算をするときは3が精度的に安心
constexpr i64 BASE = 1000;
constexpr int BASELOG = 3;
struct BigInt {
    // 掛け算のために本来の最大桁数の2倍必要
    vi digit = vi(1 << 18);
    int size;
    BigInt(int size = 1, i64 a = 0) : size(size) {
        digit[0] = a;
    }
    BigInt(const BigInt& a) {
        size = a.size;
        digit = vi(a.digit);
    }
};

bool operator<(BigInt x, BigInt y) {
    if (x.size != y.size) {
        return x.size < y.size;
    }
    for (int i = x.size - 1; i >= 0; i--) {
        if (x.digit[i] != y.digit[i]) {
            return x.digit[i] < y.digit[i];
        }
    }
    return false;
}

bool operator>(BigInt x, BigInt y) {
    return y < x;
}
bool operator<=(BigInt x, BigInt y) {
    return !(y < x);
}
bool operator>=(BigInt x, BigInt y) {
    return !(x < y);
}
bool operator!=(BigInt x, BigInt y) {
    return x < y || y < x;
}
bool operator==(BigInt x, BigInt y) {
    return !(x < y) && !(y < x);
}

BigInt normal(BigInt x, bool all = false) {
    i64 c = 0;
    if (all) {
        x.size = int(x.digit.size()) - 1;
    }
    for (int i = 0; i < x.size; i++) {
        while (x.digit[i] < 0) {
            x.digit[i + 1] -= 1;
            x.digit[i] += BASE;
        }
        while (x.digit[i] >= BASE) {
            x.digit[i + 1] += 1;
            x.digit[i] -= BASE;
        }
        i64 a = x.digit[i] + c;
        x.digit[i] = a % BASE;
        c = a / BASE;
    }
    for (; c > 0; c /= BASE) {
        x.digit[x.size++] = c % BASE;
    }
    while (x.size > 1 && x.digit[x.size - 1] == 0) {
        x.size--;
    }
    return x;
}

BigInt convert(i64 a) {
    return normal(BigInt(1, a), true);
}

BigInt convert(const string& s) {
    BigInt x;
    assert(s.size() / BASELOG <= x.digit.size() / 2);
    int i = s.size() % BASELOG;
    if (i > 0) {
        i -= BASELOG;
    }
    for (; i < int(s.size()); i += BASELOG) {
        i64 a = 0;
        for (int j = 0; j < BASELOG; j++) {
            a = 10 * a + (i + j >= 0 ? s[i + j] - '0' : 0);
        }
        x.digit[x.size++] = a;
    }
    reverse(x.digit.begin(), x.digit.begin() + x.size);
    return normal(x);
}

ostream &operator<<(ostream& os, BigInt x) {
    os << x.digit[x.size - 1];
    for (int i = x.size - 2; i >= 0; i--) {
        os << setw(BASELOG) << setfill('0') << x.digit[i];
    }
    return os;
}

istream &operator>>(istream& is, BigInt &x) {
    string s;
    is >> s;
    x = convert(s);
    return is;
}

string to_string(BigInt &x) {
    stringstream ss;
    ss << x.digit[x.size - 1];
    for (int i = x.size - 2; i >= 0; i--) {
        ss << setw(BASELOG) << setfill('0') << x.digit[i];
    }
    return ss.str();
}

BigInt operator+(BigInt x, BigInt y) {
    if (x.size < y.size) {
        x.size = y.size;
    }
    for (int i = 0; i < y.size; i++) {
        x.digit[i] += y.digit[i];
    }
    return normal(x);
}

BigInt operator-(BigInt x, BigInt y) {
    assert(x >= y);
    for (int i = 0; i < y.size; i++) {
        x.digit[i] -= y.digit[i];
    }
    return normal(x);
}

BigInt operator*(BigInt x, i64 a) {
    for (int i = 0; i < x.size; i++) {
        x.digit[i] *= a;
    }
    return normal(x);
}

void fft(vector<complex<double>>& a, bool inv = false) {
    int n = int(a.size());
    if (n == 1) return;
    vector<complex<double>> even(n / 2), odd(n / 2);
    for (int i = 0; i < n / 2; i++) {
        even[i] = a[2 * i];
        odd[i] = a[2 * i + 1];
    }
    fft(even, inv);
    fft(odd, inv);
    complex<double> omega = polar(1.0, (-2 * inv + 1) * 2 * acos(-1) / n);
    complex<double> pow_omega = 1.0;
    for (int i = 0; i < n / 2; i++) {
        a[i] = even[i] + pow_omega * odd[i];
        a[i + n / 2] = even[i] - pow_omega * odd[i];
        pow_omega *= omega;
    }
}

void conv(vector<complex<double>>& a, vector<complex<double>>& b) {
    fft(a);
    fft(b);
    int n = int(a.size());
    for (int i = 0; i < n; i++) {
        a[i] *= b[i] / complex<double>(n);
    }
    fft(a, true);
}

void conv(vi& a, vi& b) {
    vector<complex<double>> ac, bc;
    for (int i = 0; i < a.size(); i++) {
        ac.push_back(a[i]);
        bc.push_back(b[i]);
    }
    conv(ac, bc);
    a.resize(ac.size());
    for (int i = 0; i < ac.size(); i++) {
        a[i] = long(real(ac[i]) + 0.5);
    }
}

BigInt operator*(BigInt x, BigInt y) {
    conv(x.digit, y.digit);
    return normal(x, true);
}

pair<BigInt, i64> divmod(BigInt x, i64 a) {
    i64 c = 0, t;
    for (int i = x.size - 1; i >= 0; i--) {
        t = BASE * c + x.digit[i];
        x.digit[i] = t / a;
        c = t % a;
    }
    return pair<BigInt, i64>(normal(x), c);
}

BigInt operator/(BigInt x, i64 a) {
    return divmod(x, a).first;
}

i64 operator%(BigInt x, i64 a) {
    return divmod(x, a).second;
}

int main() {
    string n, k;
    cin >> n >> k;
    BigInt N = convert(n), K = convert(k);
    int a = N % 6;
    int b = K % 2;
    string hoge = "285714";
    if (a == 0) {
        cout << hoge[5] << endl;
        return 0;
    } else if (a == 1) {
        cout << 2 << endl;
        return 0;
    } else if (a == 2) {
        cout << (b == 0 ? hoge[3] : hoge[1]) << endl;
        return 0;
    } else if (a == 3) {
        cout << hoge[2] << endl;
        return 0;
    } else if (a == 4) {
        cout << hoge[3] << endl;
        return 0;
    } else {
        cout << (b == 0 ? hoge[0] : hoge[4]) << endl;
        return 0;
    }
}










































0