CODE FESTIVAL 2016 Final

Submission #5425786

Source codeソースコード

#include <bits/stdc++.h>

using namespace std;

template<int mod> struct ModInt {
	using ll = long long;
	int x;
	template<class T>
	ModInt(T a) {
		x = a % mod;
		if (x < 0) {
			x += mod;
		}
	}
	ModInt() : x(0) {}
	inline ModInt& operator+=(const ModInt& rhs)
	{
		(*this) += rhs.x;
		return *this;
	}
	template<class T>
	inline ModInt& operator+=(const T rhs)
	{
		x += rhs % mod;
		if (x < 0) x += mod;
		x %= mod;
		return *this;
	}
	inline ModInt& operator-=(const ModInt& rhs)
	{
		(*this) -= rhs.x;
		return *this;
	}
	template<class T>
	inline ModInt& operator-=(const T rhs)
	{
		x -= rhs % mod;
		if (x < 0) x += mod;
		x %= mod;
		return *this;
	}
	inline ModInt& operator*=(const ModInt& rhs)
	{
		(*this) *= rhs.x;
		return *this;
	}
	template<class T>
	inline ModInt& operator*=(const T rhs)
	{
		ll res = (ll) x * (rhs % mod);
		x = res % mod;
		if (x < 0) x += mod;
		return *this;
	}
	inline ModInt& operator/=(const ModInt& rhs)
	{
		(*this) /= rhs.x;
		return *this;
	}
	template<class T>
	inline ModInt& operator/=(const T rhs)
	{
		int t = rhs % mod;
		if (t < 0) t += mod;
		ll res = modpow(t);
		(*this) *= res;
		return *this;
	}
	inline ModInt& operator=(const ModInt& rhs)
	{
		(*this) = rhs.x;
		return *this;
	}
	template<class T>
	inline ModInt& operator=(const T rhs)
	{
		x = rhs % mod;
		if (x < 0) x += mod;
		return *this;
	}
	inline int operator==(const ModInt& rhs) const
	{
		return (*this) == rhs.x;
	}
	template<class T>
	inline int operator==(const T rhs) const
	{
		ModInt t(rhs);
		return (*this).x == t.x;
	}
	inline int operator!=(const ModInt& rhs) const
	{
		return (*this) != rhs.x;
	}
	inline int operator!=(const int rhs) const
	{
		ModInt t(rhs);
		return (*this).x != t.x;
	}
	inline ModInt operator++(signed unused)
	{
		ModInt res((*this).x);
		++(*this);
		return res;
	}
	inline ModInt& operator++()
	{
		(*this) += 1;
		return (*this);
	}
	inline ModInt operator--(signed unused)
	{
		ModInt res((*this).x);
		--(*this);
		return res;
	}
	inline ModInt& operator--()
	{
		(*this) -= 1;
		return (*this);
	}
	inline ModInt operator+() const
	{
		return (*this);
	}
	inline ModInt operator-() const
	{
		return (*this).x ? ModInt(mod - (*this).x) : ModInt(0);
	}
	template<class T>
	int modpow(const T val, int p = mod - 2)
	{
		if (p == 0) return 1;
		if (p % 2) return (long long) val * modpow(val, p-1) % mod;
		long long t = modpow(val, p/2);
		int res = t * t % mod;
		return res;
	}
	operator int() const
	{
		return x;
	}
	friend ostream& operator<<(ostream& lhs, const ModInt& rhs)
	{
		lhs << rhs.x;
		return lhs;
	}
	friend istream& operator>>(istream& lhs, ModInt& rhs)
	{
		long long t;
		lhs >> t;
		rhs.x = t % mod;
		if (rhs.x < 0) rhs += mod;
		return lhs;
	}
	friend const ModInt operator+(const ModInt& lhs, const ModInt& rhs)  {return ModInt(lhs) += rhs;}
	template<class T>
	friend const ModInt operator+(const ModInt& lhs, const T rhs)        {return ModInt(lhs) += rhs;}
	template<class T>
	friend const ModInt operator+(T lhs, const ModInt& rhs)              {return ModInt(lhs) += rhs;}
	friend const ModInt operator-(const ModInt& lhs, const ModInt& rhs)  {return ModInt(lhs) -= rhs;}
	template<class T>
	friend const ModInt operator-(const ModInt& lhs, const T rhs)        {return ModInt(lhs) -= rhs;}
	template<class T>
	friend const ModInt operator-(T lhs, const ModInt& rhs)              {return ModInt(lhs) -= rhs;}
	friend const ModInt operator*(const ModInt& lhs, const ModInt& rhs)  {return ModInt(lhs) *= rhs;}
	template<class T>
	friend const ModInt operator*(const ModInt& lhs, const T rhs)        {return ModInt(lhs) *= rhs;}
	template<class T>
	friend const ModInt operator*(T lhs, const ModInt& rhs)              {return ModInt(lhs) *= rhs;}
	friend const ModInt operator/(const ModInt& lhs, const ModInt& rhs)  {return ModInt(lhs) /= rhs;}
	template<class T>
	friend const ModInt operator/(const ModInt& lhs, const T rhs)        {return ModInt(lhs) /= rhs;}
	template<class T>
	friend const ModInt operator/(T lhs, const ModInt& rhs)              {return ModInt(lhs) /= rhs;}
	template<class T>
	friend const int    operator==(T lhs, const ModInt& rhs)             {return ModInt(lhs) == rhs;}
	template<class T>
	friend const int    operator!=(T lhs, const ModInt& rhs)             {return ModInt(lhs) != rhs;}
};
using modint = ModInt<1000000007>;

// i日目
// j孤立点
// k成分
modint dp[301][301][301];

#define reps(i, n, m) for (int i = n; i < m; i++)
#define rep(i, n) reps(i, 0, n)

int main()
{
  int n, m;
  cin >> n >> m;
  dp[0][n-1][1] = 1;
  rep(i, m) {
    rep(j, n+1) {
      rep(k, n+1) {
        int e = n - k - j;
        if (e < 0) continue;
        if (j) {
          dp[i+1][j-1][k] += j * dp[i][j][k];
        } else {
          dp[i+1][j-1][1] += j * dp[i][j][k];
        }
        dp[i+1][j][k] += e * dp[i][j][k];
        dp[i+1][j][k+e] += k * dp[i][j][k];
      }
    }
  }
  cout << dp[m][0][n] << endl;
}

Submission

Task問題 F - Road of the King
User nameユーザ名 spihill
Created time投稿日時
Language言語 C++14 (GCC 5.4.1)
Status状態 AC
Score得点 1000
Source lengthソースコード長 5149 Byte
File nameファイル名
Exec time実行時間 364 ms
Memory usageメモリ使用量 106752 KB

Test case

Set

Set name Score得点 / Max score Cases
sample - sample-01.txt,sample-02.txt,sample-03.txt
all 1000 / 1000 sample-01.txt,sample-02.txt,sample-03.txt,01-01.txt,01-02.txt,01-03.txt,01-04.txt,01-05.txt,01-06.txt,01-07.txt,01-08.txt,01-09.txt,01-10.txt,sample-01.txt,sample-02.txt,sample-03.txt

Test case

Case name Status状態 Exec time実行時間 Memory usageメモリ使用量
01-01.txt AC 33 ms 106752 KB
01-02.txt AC 33 ms 106752 KB
01-03.txt AC 33 ms 106752 KB
01-04.txt AC 34 ms 106752 KB
01-05.txt AC 34 ms 106752 KB
01-06.txt AC 65 ms 106752 KB
01-07.txt AC 342 ms 106752 KB
01-08.txt AC 360 ms 106752 KB
01-09.txt AC 362 ms 106752 KB
01-10.txt AC 364 ms 106752 KB
sample-01.txt AC 33 ms 106752 KB
sample-02.txt AC 119 ms 106752 KB
sample-03.txt AC 199 ms 106752 KB