結果

問題 No.391 CODING WAR
ユーザー drken1215drken1215
提出日時 2018-01-16 19:16:02
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 264 ms / 2,000 ms
コード長 3,683 bytes
コンパイル時間 1,385 ms
コンパイル使用メモリ 90,040 KB
実行使用メモリ 6,812 KB
最終ジャッジ日時 2023-08-25 19:51:42
合計ジャッジ時間 4,601 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
6,812 KB
testcase_01 AC 13 ms
6,636 KB
testcase_02 AC 13 ms
6,620 KB
testcase_03 AC 13 ms
6,632 KB
testcase_04 AC 13 ms
6,624 KB
testcase_05 AC 14 ms
6,620 KB
testcase_06 AC 13 ms
6,632 KB
testcase_07 AC 13 ms
6,744 KB
testcase_08 AC 13 ms
6,640 KB
testcase_09 AC 264 ms
6,680 KB
testcase_10 AC 249 ms
6,676 KB
testcase_11 AC 169 ms
6,812 KB
testcase_12 AC 12 ms
6,628 KB
testcase_13 AC 245 ms
6,628 KB
testcase_14 AC 205 ms
6,632 KB
testcase_15 AC 226 ms
6,620 KB
testcase_16 AC 140 ms
6,744 KB
testcase_17 AC 163 ms
6,620 KB
testcase_18 AC 114 ms
6,632 KB
testcase_19 AC 116 ms
6,640 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; }


inline long long mod(long long a, long long m) { return (a % m + m) % m; }
struct Fp {
	int MOD = 1000000007;
	long long val;

	Fp() : val(0) {}
	Fp(long long val_) { this->val = mod(val_, MOD); }
	Fp operator = (long long val_) { this->val = mod(val_, MOD); return *this; }
	inline Fp operator - () { return mod(-val, MOD); }
	inline const Fp& operator += (const Fp &x);
	inline const Fp& operator -= (const Fp &x);
	inline const Fp& operator *= (const Fp &x);
	inline const Fp& operator /= (const Fp &x);
};

inline bool operator == (Fp x, Fp y) { return x.val == y.val; }
inline bool operator != (Fp x, Fp y) { return !(x == y); }

ostream &operator << (ostream &os, Fp x) { return os << x.val; }
istream &operator >> (istream &is, Fp &x) { is >> x; return is; }

inline Fp operator + (Fp x, Fp y) { return mod(x.val + y.val, x.MOD); }
inline Fp operator - (Fp x, Fp y) { return mod(x.val - y.val, x.MOD); }
inline Fp operator * (Fp x, Fp y) { return mod(x.val * y.val, x.MOD); }
inline Fp operator / (Fp x, Fp y) {
	long long a = y.val, b = x.MOD, u = 1, v = 0;
	while (b) {
		long long t = a / b;
		a -= t*b; swap(a, b);
		u -= t*v; swap(u, v);
	}
	return x * u;
}
inline Fp abs(Fp a) { return a; }
inline Fp fpow(Fp a, long long n) {
	if (n == 0) return Fp(1);
	Fp t = fpow(a, n / 2);
	t = t * t; if (n & 1) t = t * a;
	return t;
}
inline const Fp& Fp::operator += (const Fp &x) { *this = *this + x; return *this; }
inline const Fp& Fp::operator -= (const Fp &x) { *this = *this - x; return *this; }
inline const Fp& Fp::operator *= (const Fp &x) { *this = *this * x; return *this; }
inline const Fp& Fp::operator /= (const Fp &x) { *this = *this / x; return *this; }

// calc comb (small n, r ver)
const int FACT_MAX = 210000;
static Fp fp_fact_val[FACT_MAX];

void calcFact(int MAX = FACT_MAX) {
	fp_fact_val[0] = 1;
	for (int val = 1; val < MAX; ++val) {
		fp_fact_val[val] = fp_fact_val[val - 1] * val;
	}
}

Fp fact(int n) {
	return fp_fact_val[n];
}

Fp com(int n, int r) {
	return fact(n) / fact(r) / fact(n - r);
}


long long N;
int K;

//S(n, k) = sum((-1)^(k-i) com(k, i) i^n, i: 1~k) / k! 
int main() {
	calcFact();
	while (cin >> N >> K) {
		Fp res = 0;
		for (int i = 1; i <= K; ++i) {
			Fp tmp = com(K, i) * fpow(i, N);
			if ((K - i) & 1) tmp = -tmp;
			res += tmp;
		}
		cout << res << endl;
	}
}







0