結果

問題 No.147 試験監督(2)
ユーザー antaanta
提出日時 2015-02-09 01:41:55
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 146 ms / 2,000 ms
コード長 4,862 bytes
コンパイル時間 633 ms
コンパイル使用メモリ 83,340 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-23 11:29:06
合計ジャッジ時間 1,815 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 146 ms
6,816 KB
testcase_01 AC 146 ms
6,944 KB
testcase_02 AC 144 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:158:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  158 |         scanf("%d", &N);
      |         ~~~~~^~~~~~~~~~
main.cpp:163:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  163 |                 scanf("%lld%s", &C, D);
      |                 ~~~~~^~~~~~~~~~~~~~~~~

ソースコード

diff #

#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cctype>
#include <cassert>
#include <limits>
#include <functional>
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r,v) auto r = (v)
#else
#define aut(r,v) __typeof(v) r = (v)
#endif
#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)
#define all(o) (o).begin(), (o).end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define mset(m,v) memset(m,v,sizeof(m))
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
using namespace std;
typedef vector<int> vi; typedef pair<int,int> pii; typedef vector<pair<int,int> > vpii; typedef long long ll;
template<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; }

template<int MOD>
struct ModInt {
	static const int Mod = MOD;
	unsigned x;
	ModInt(): x(0) { }
	ModInt(signed sig) { int sigt = sig % MOD; if(sigt < 0) sigt += MOD; x = sigt; }
	ModInt(signed long long sig) { int sigt = sig % MOD; if(sigt < 0) sigt += MOD; x = sigt; }
	int get() const { return (int)x; }
	
	ModInt &operator+=(ModInt that) { if((x += that.x) >= MOD) x -= MOD; return *this; }
	ModInt &operator-=(ModInt that) { if((x += MOD - that.x) >= MOD) x -= MOD; return *this; }
	ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; }
	ModInt &operator/=(ModInt that) { return *this *= that.inverse(); }
	
	ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
	ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
	ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
	ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
	
	ModInt inverse() const {
		signed a = x, b = MOD, u = 1, v = 0;
		while(b) {
			signed t = a / b;
			a -= t * b; std::swap(a, b);
			u -= t * v; std::swap(u, v);
		}
		if(u < 0) u += Mod;
		ModInt res; res.x = (unsigned)u;
		return res;
	}
};
template<int MOD> ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) {
	ModInt<MOD> r = 1;
	while(k) {
		if(k & 1) r *= a;
		a *= a;
		k >>= 1;
	}
	return r;
}
typedef ModInt<1000000007> mint;

int digitsMod(char s[], int n, int m) {
	int x = 0; long long pow10 = 1;
	for(int i = n-1; i >= 0; i --) {
		if((x += pow10 * (s[i] - '0') % m) >= m) x -= m;
		pow10 = pow10 * 10 % m;
	}
	return x;
}

struct Matrix {
	typedef mint Num;
	static const int MaxN = 2;
	int hei, wid;
	Num v[MaxN][MaxN];
	Matrix() {}
	Matrix(int n, int m): hei(n), wid(m) { mset(v, 0); }
	inline int height() const { return hei; }
	inline int width() const { return wid; }
	inline Num& at(int i, int j) { return v[i][j]; }
	inline const Num& at(int i, int j) const { return v[i][j]; }
	static Matrix identity(int n) {
		Matrix A(n, n);
		rep(i, n) A.at(i, i) = 1;
		return A;
	}
	inline static Matrix identity(const Matrix& A) { return identity(A.height()); }
	Matrix& operator*=(const Matrix& B) {
		int n = height(), m = B.width(), p = B.height();
		assert(p == width());
		const unsigned (*b)[MaxN] = reinterpret_cast<const unsigned (*)[MaxN]>(B.v);
		Num w[MaxN][MaxN];
		rep(i, n) {
			const unsigned *ai = reinterpret_cast<const unsigned*>(v[i]);
			rep(j, m) {
				unsigned long long x = 0;
				rep(k, p) x += (unsigned long long)ai[k] * b[k][j];
				w[i][j].x = x % mint::Mod;
			}
		}
		memcpy(v, w, sizeof(v));
		return *this;
	}
};

Matrix operator^(const Matrix& t, ll k) {
	Matrix A = t, B = Matrix::identity(t);
	while(k) {
		if(k & 1) B *= A;
		A *= A;
		k >>= 1;
	}
	return B;
}

int fibonacci(long long n, int Mod) {
	int m2 = 1, m1 = 1, m0 = 0;
	int x2 = 1, x1 = 0, x0 = 1;
	while(n > 0) {
		if (n & 1) {
			int a2 = ((long long)x2 * m2 + (long long)x1 * m1) % Mod;
			int a1 = ((long long)x2 * m1 + (long long)x1 * m0) % Mod;
			int a0 = ((long long)x1 * m1 + (long long)x0 * m0) % Mod;
			x2 = a2, x1 = a1, x0 = a0;
		}
		n >>= 1;
		if (n > 0) {
			int a2 = ((long long)m2 * m2 + (long long)m1 * m1) % Mod;
			int a1 = ((long long)m2 * m1 + (long long)m1 * m0) % Mod;
			int a0 = ((long long)m1 * m1 + (long long)m0 * m0) % Mod;
			m2 = a2, m1 = a1, m0 = a0;
		}
	}
	return x1 % Mod;
}


int main() {
	int N;
	scanf("%d", &N);
	char *D = new char[202];
	mint ans = 1;
	rep(i, N) {
		long long C;
		scanf("%lld%s", &C, D);
		mint f = fibonacci(C+2, mint::Mod);
		int d = digitsMod(D, strlen(D), mint::Mod-1);
		mint t = d == 0 && f.get() == 0 ? 0 : f ^ d;
		ans *= t;
	}
	printf("%d\n", ans.get());
	return 0;
}
0