結果

問題 No.303 割れません
ユーザー heno239heno239
提出日時 2020-08-25 13:48:10
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 9,514 bytes
コンパイル時間 2,224 ms
コンパイル使用メモリ 140,572 KB
実行使用メモリ 88,584 KB
最終ジャッジ日時 2024-11-06 11:20:42
合計ジャッジ時間 52,870 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
7,752 KB
testcase_01 AC 4 ms
7,748 KB
testcase_02 AC 4 ms
7,748 KB
testcase_03 AC 3,288 ms
49,668 KB
testcase_04 AC 6,557 ms
87,272 KB
testcase_05 AC 6,523 ms
88,584 KB
testcase_06 AC 6,518 ms
87,856 KB
testcase_07 AC 3,293 ms
87,600 KB
testcase_08 AC 3,421 ms
87,320 KB
testcase_09 AC 3,392 ms
87,424 KB
testcase_10 WA -
testcase_11 AC 6,562 ms
87,588 KB
testcase_12 AC 3,419 ms
87,456 KB
testcase_13 AC 3,776 ms
48,176 KB
testcase_14 AC 1,345 ms
47,224 KB
testcase_15 AC 1,682 ms
27,820 KB
testcase_16 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-12;
const ld pi = acosl(-1.0);

ll mod_pow(ll x, ll n, ll m) {
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	ll n;
	modint() :n(0) { ; }
	modint(ll m) :n(m) {
		if (n >= mod)n %= mod;
		else if (n < 0)n = (n % mod + mod) % mod;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }

const int max_n = 1 << 18;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}

LDP operator*(const LDP& a, const LDP& b) {
	return LDP{ a.first * b.first - a.second * b.second , a.first * b.second + a.second * b.first };
}
LDP operator+(const LDP& a, const LDP& b) {
	return LDP{ a.first + b.first,a.second + b.second };
}
LDP operator-(const LDP& a, const LDP& b) {
	return LDP{ a.first - b.first,a.second - b.second };
}
//非再帰
//eps=0.01を忘れずに
typedef vector<LDP> poly;
poly dft(poly f, bool inverse = false) {
	int n = f.size(); int i, j, k;
	//bit左右反転
	for (i = 0, j = 1; j < n - 1; j++) {
		for (k = n >> 1; k > (i ^= k); k >>= 1);
		if (i > j)swap(f[i], f[j]);
	}
	for (int m = 2; m <= n; m *= 2) {
		LDP zeta = { cos(2 * pi / (ld)m), sin(2 * pi / (ld)m) };
		if (inverse) {
			zeta = { cos(2 * pi * (m - 1) / (ld)m), sin(2 * pi * (m - 1) / (ld)m) };
		}
		for (i = 0; i < n; i += m) {
			LDP powzeta = { 1,0 };
			for (k = i; k < i + m / 2; k++) {
				LDP t1 = f[k], t2 = powzeta * f[k + m / 2];
				f[k] = t1 + t2; f[k + m / 2] = t1 - t2;
				powzeta = powzeta * zeta;
			}
		}
	}
	if (inverse) {
		for (i = 0; i < n; i++) {
			f[i].first /= (ld)n;
			f[i].second /= (ld)n;
		}
	}
	return f;
}
poly multiply(poly g, poly h) {
	int n = 1; int sz = g.size() + h.size();
	while (n <= sz)n *= 2;
	while (g.size() < n) {
		g.push_back({ 0,0 });
	}
	while (h.size() < n) {
		h.push_back({ 0,0 });
	}
	poly gg = dft(g);
	poly hh = dft(h);
	poly ff;
	rep(i, n) {
		ff.push_back(gg[i] * hh[i]);
	}
	return dft(ff, true);
}

struct bigint {
	using vll = vector<ll>;

	constexpr static ll base = 1000000000;
	constexpr static ll base_digits = 9;

	vll a;
	ll sign;

	bigint() :sign(1) {}
	bigint(ll v) { *this = v; }

	bigint(const string& s) { read(s); }

	void read(const string& s) {
		sign = 1;
		a.clear();
		int pos = 0;
		while (pos < s.size() && (s[pos] == '-' || s[pos] == '+')) {
			if (s[pos] == '-')sign = -sign;
			pos++;
		}
		for (int i = s.size() - 1; i >= pos; i -= base_digits) {
			ll x = 0;
			int le = i - base_digits + 1;
			if (le < pos)le = pos;
			for (int j = le; j <= i; j++) {
				x = x * 10 + s[j] - '0';
			}
			a.push_back(x);
		}
		trim();
	}

	friend istream& operator>>(istream& stream, bigint& v) {
		string s; stream >> s;
		v.read(s);
		return stream;
	}
	friend ostream& operator<<(ostream& stream, const bigint& v) {
		if (v.sign == -1)stream << '-';
		if (v.a.size())stream << v.a.back();
		else stream << 0;
		for (int i = (int)v.a.size() - 2; i >= 0; --i) {
			stream << setw(base_digits) << setfill('0') << v.a[i];
		}
		return stream;
	}

	void operator=(ll v) {
		sign = 1;
		if (v < 0)sign = -1, v = -v;
		while (v > 0) {
			a.push_back(v % base); v /= base;
		}
	}
	void trim() {
		while (!a.empty() && a.back() == 0)a.pop_back();
		if (a.empty())sign = 1;
	}


	bigint operator-()const {
		bigint res = *this;
		res.sign = -sign;
		return res;
	}
	bigint abs()const {
		bigint res = *this;
		res.sign *= res.sign;
		return res;
	}
	bool operator<(const bigint& v)const {
		if (sign != v.sign)return sign < v.sign;
		if (a.size() != v.a.size())return a.size() * sign < v.a.size()* sign;
		per(i, (int)a.size()) {
			if (a[i] != v.a[i])return a[i] * sign < v.a[i] * sign;
		}
		return false;
	}
	//comparation
	bool operator>(const bigint& v)const {
		return v < *this;
	}
	bool operator<=(const bigint& v)const {
		return !(v < *this);
	}
	bool operator>=(const bigint& v)const {
		return !(*this < v);
	}
	bool operator==(const bigint& v)const {
		return !(*this < v) && !(v < *this);
	}
	bool operator!=(const bigint& v)const {
		return *this < v || v < *this;
	}

	//calcuration
	bigint operator+(const bigint& v)const {
		if (sign == v.sign) {
			bigint res = v;
			ll c = 0;
			int i = 0;
			while (i < a.size() || i < v.a.size() || c>0) {
				if (i == res.a.size())res.a.push_back(0);
				res.a[i] += c;
				if (i < a.size())res.a[i] += a[i];
				c = res.a[i] >= base;
				if (c)res.a[i] -= base;
				i++;
			}
			return res;
		}
		return *this - (-v);
	}
	bigint operator-(const bigint& v)const {
		if (sign == v.sign) {
			if (abs() >= v.abs()) {
				//cout << (*this) << "\n";
				//cout << v << "\n";
				bigint res = *this;
				ll c = 0;
				int i = 0;
				while (i < v.a.size() || c) {
					res.a[i] -= c;
					if (i < v.a.size())res.a[i] -= v.a[i];
					c = res.a[i] < 0;
					if (c)res.a[i] += base;

					i++;
				}
				res.trim();
				return res;
			}
			return -(v - *this);
		}
		return *this + (-v);
	}

	static vll convert_base(const vll& a, int old_digits, int new_digits) {
		vll p(max(old_digits, new_digits) + 1);
		p[0] = 1;
		for (int i = 1; i < p.size(); i++)p[i] = p[i - 1] * 10;
		vll res;
		ll cur = 0;
		int cur_digits = 0;
		rep(i, a.size()) {
			cur += a[i] * p[cur_digits];
			cur_digits += old_digits;
			while (cur_digits >= new_digits) {
				res.push_back(signed(cur % p[new_digits]));
				cur /= p[new_digits];
				cur_digits -= new_digits;
			}
		}
		res.push_back(signed(cur));
		while (!res.empty() && !res.back())res.pop_back();
		return res;
	}
	bigint operator*(const bigint& v)const {
		constexpr static ll nbase = 100;
		constexpr static ll nbase_digits = 2;

		vll a = convert_base(this->a, base_digits, nbase_digits);
		vll b = convert_base(v.a, base_digits, nbase_digits);

		if (a.empty())a.push_back(0);
		if (b.empty())b.push_back(0);

		poly p(a.size()); rep(i, a.size())p[i] = { a[i],0 };
		poly q(b.size()); rep(i, b.size())q[i] = { b[i],0 };
		poly r = multiply(p, q);

		bigint res;
		res.sign = sign * v.sign;
		int i = 0; ll c = 0;
		rep(i, r.size()) {
			ll cur = (ll)(r[i].first + 0.01) + c;
			res.a.push_back((ll)(cur % nbase));
			c = (ll)(cur / nbase);
			if (i + 1 == r.size() && c > 0)r.push_back({ 0,0 });
		}

		res.a = convert_base(res.a, nbase_digits, base_digits);
		res.trim();
		return res;
	}
};



typedef vector<vector<bigint>> mat;
typedef vector<bigint> vec;
mat mtmul(mat& A, mat& B) {
	mat C(A.size(), vec(B[0].size(), 0));
	rep(i, (int)A.size()) {
		rep(k, (int)B.size()) {
			rep(j, (int)B[0].size()) {
				C[i][j] = (C[i][j] + (A[i][k] * B[k][j]));
			}
		}
	}
	return C;
}
mat mtpow(mat A, ll n) {
	mat B(A.size(), vec(A.size(), 0));
	rep(i, (int)A.size()) {
		B[i][i] = 1;
	}
	while (n > 0) {
		if (n & (ll)1)B = mtmul(B, A);
		A = mtmul(A, A);
		n >>= 1;
	}
	return B;
}


bigint dp[2];
bigint calc(ll n) {
	mat A = { {1,1},{1,0} };
	A = mtpow(A, n);
	return A[0][1];
}
bigint calc2(ll n) {
	bigint c1 = 1, c2 = 1;
	for (int i = 3; i <= n; i++) {
		cout << i << "\n";
		cout << c1 << " " << c2 << " " << (c1 + c2) << "\n";
		c1 = c1 + c2;
		swap(c1, c2);
	}
	return c2;
}
void solve() {
	ll n; cin >> n;
	if (n == 2) {
		cout << "INF\n";
		cout << 0 << "\n";
		return;
	}
	bigint ans = calc(n);
	//cout << ans << "\n";
	if (n % 2 == 0)ans = ans - calc(n / 2) * calc(n / 2);
	cout << n << "\n";
	cout << ans << "\n";
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(15);
	//init_f();
	//expr();
	//int t; cin >> t; rep(i, t)
	solve();
	return 0;
}
0