結果

問題 No.890 移調の限られた旋法
ユーザー MarcusAureliusAntoninus
提出日時 2019-09-20 22:47:00
言語 C++17(1z)
(gcc 9.2.0)
結果
AC  
実行時間 80 ms
コード長 3,770 Byte
コンパイル時間 1,800 ms
使用メモリ 33,976 KB
最終ジャッジ日時 2020-01-28 02:13:27

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
0_sample01.txt AC 52 ms
26,668 KB
0_sample02.txt AC 56 ms
26,840 KB
0_sample03.txt AC 52 ms
26,844 KB
1_small01.txt AC 48 ms
26,704 KB
1_small02.txt AC 52 ms
26,588 KB
1_small03.txt AC 56 ms
26,912 KB
1_small04.txt AC 52 ms
26,624 KB
1_small05.txt AC 56 ms
26,752 KB
1_small06.txt AC 56 ms
26,700 KB
1_small07.txt AC 48 ms
26,636 KB
1_small08.txt AC 60 ms
26,632 KB
1_small09.txt AC 52 ms
26,756 KB
1_small10.txt AC 48 ms
26,592 KB
2_large01.txt AC 80 ms
33,924 KB
2_large02.txt AC 68 ms
33,964 KB
2_large03.txt AC 76 ms
33,976 KB
2_large04.txt AC 76 ms
33,944 KB
2_large05.txt AC 68 ms
33,212 KB
2_large06.txt AC 72 ms
33,156 KB
2_large07.txt AC 60 ms
30,248 KB
2_large08.txt AC 64 ms
28,984 KB
2_large09.txt AC 56 ms
26,848 KB
2_large10.txt AC 64 ms
31,988 KB
2_large11.txt AC 72 ms
33,612 KB
2_large12.txt AC 64 ms
30,512 KB
2_large13.txt AC 52 ms
27,424 KB
2_large14.txt AC 72 ms
33,660 KB
2_large15.txt AC 72 ms
33,972 KB
2_large16.txt AC 64 ms
31,036 KB
2_large17.txt AC 64 ms
29,544 KB
2_large18.txt AC 72 ms
33,080 KB
2_large19.txt AC 64 ms
30,272 KB
2_large20.txt AC 76 ms
32,644 KB
2_large21.txt AC 76 ms
33,260 KB
2_large22.txt AC 72 ms
33,228 KB
テストケース一括ダウンロード

ソースコード

diff #
#include <bits/stdc++.h>

////////////
// ModInt //
////////////

// 四則演算の最も左に存在する値がModIntでなければキャストでバグる
// 例えばx = mint * 1000;やx = ModInt(1000) * mint;はいいがx = 1000 * mint;は駄目。
template<int64_t mod_ = 1'000'000'007>
class ModInt {
private:
	int64_t integer_;

public:
	constexpr ModInt(const int64_t initial_number = 0)
		: integer_(initial_number){}
	
	// 四則演算
	constexpr ModInt operator+(const ModInt& operand) const
	{
		ModInt ret{this->integer_ + operand.integer_};
		if (ret.integer_ >= mod_)
			ret.integer_ -= mod_;
		return ret;
	}
	constexpr ModInt operator-(const ModInt& operand) const
	{
		ModInt ret{this->integer_ - operand.integer_};
		if (ret.integer_ < 0)
			ret.integer_ += mod_;
		return ret;
	}
	constexpr ModInt operator*(const ModInt& operand) const
	{
		return {this->integer_ * operand.integer_ % mod_};
	}
	constexpr ModInt operator/(const ModInt& operand) const
	{
		return *this * (operand ^ (mod_ - 2));
	}

	// 単項演算子
	constexpr ModInt& operator++()
	{
		if (integer_ + 1 == mod_) integer_ = 0;
		else integer_++;
		return *this;
	}

	constexpr ModInt& operator--()
	{
		if (integer_ == 0) integer_ = mod_ - 1;
		else integer_--;
		return *this;
	}

	constexpr ModInt operator+()
	{
		return *this;
	}

	constexpr ModInt operator-()
	{
		if (integer_ == 0) return ModInt(0ll);
		else return ModInt(mod_ - integer_);
	}

	// 累乗
	constexpr ModInt operator^(const int64_t operand) const
	{
		ModInt ret{1}, pow_ope{this->integer_};
		for (int64_t pow{operand}; pow > 0; pow >>= 1)
		{
			if (pow & 1) ret *= pow_ope;
			pow_ope *= pow_ope;
		}
		return ret;
	}

	// 代入
	constexpr ModInt& operator=(const ModInt& operand)
	{
		this->integer_ = operand.integer_;
		return *this;
	}
	constexpr ModInt& operator+=(const ModInt& operand)
	{
		*this = *this + operand;
		return *this;
	}
	constexpr ModInt& operator-=(const ModInt& operand)
	{
		*this = *this - operand;
		return *this;
	}
	constexpr ModInt& operator*=(const ModInt& operand)
	{
		*this = *this * operand;
		return *this;
	}
	constexpr ModInt& operator/=(const ModInt& operand)
	{
		*this = *this / operand;
		return *this;
	}

	// その他
	constexpr operator int64_t() { return integer_; }

	constexpr ModInt getOne() const
	{
		return ModInt(1ll);
	}
	constexpr ModInt getZero() const
	{
		return ModInt(0ll);
	}
};
using Mint = ModInt<>;

////////////////
// 組み合わせ //
///////////////

template<int64_t mod_ = 1'000'000'007, int max_ = 200'000>
class Combination {
public:
	std::array<ModInt<mod_>, max_ + 1> inv, fact, finv;

	Combination()
	{
		inv[0] = inv[1] = fact[0] = fact[1] = finv[0] = finv[1] = 1;
		for (int num{2}; num <= max_; num++)
		{
			inv[num] = (mod_ - (int64_t)inv[mod_ % num] * (mod_ / num) % mod_) % mod_;
			fact[num] = num * fact[num - 1] % mod_;
			finv[num] = inv[num] * finv[num - 1] % mod_;
		}
	}

	constexpr ModInt<mod_> getCombi(const int n, const int r) const
	{
		if (r < 0 || n < 0 || n - r < 0) return 0;
		return fact[n] * finv[r] * finv[n - r];
	}

	constexpr ModInt<mod_> getPerm(const int n, const int r) const
	{
		if (r < 0 || n < 0 || n - r < 0) return 0;
		return fact[n] * finv[n - r];
	}
};
Combination<1'000'000'007, 1'000'000> combi;

int64_t calcGCD(int64_t a, int64_t b)
{
	while (a)
	{
		b %= a;
		std::swap(a, b);
	}
	return b;
}

int main()
{
	int64_t N, K;
	scanf("%lld%lld", &N, &K);
	std::vector<Mint> table(N + 1);
	for (int64_t i{N}; i > 1; i--)
	{
		if (N % i > 0 || K % i > 0) continue;
		table[i] = combi.getCombi(N / i, K / i);
		for (int64_t j{2 * i}; j <= N; j += i)
				table[i] -= table[j];
	}
	Mint sum;
	for (int i{2}; i <= N; i++)
		sum += table[i];
	std::cout << sum << std::endl;

	return 0;
}
0