Problem:
Balls and Boxes 1
Balls
Boxes
Any way
At most one ball
At least one ball
Distinguishable
Distinguishable
1
2
3
Indistinguishable
Distinguishable
4
5
6
Distinguishable
Indistinguishable
7
8
9
Indistinguishable
Indistinguishable
10
11
12
Problem
You have $n$ balls and $k$ boxes. You want to put these balls into the boxes.
Find the number of ways to put the balls under the following conditions:
Each ball is distinguished from the other.
Each box is distinguished from the other.
Each ball can go into only one box and no one remains outside of the boxes.
Each box can contain an arbitrary number of balls (including zero).
Note that you must print this count modulo $10^9+7$.
Input
$n$ $k$
The first line will contain two integers $n$ and $k$.
Output
Print the number of ways modulo $10^9+7$ in a line.
Constraints
$1 \le n \le 1000$
$1 \le k \le 1000$
Sample Input 1
2 3
Sample Output 1
9
Sample Input 2
10 5
Sample Output 2
9765625
Sample Input 3
100 100
Sample Output 3
424090053

Submitted solutions:

code_snippet: /*
    Created by Pujx on 2024/8/4.
*/
#pragma GCC optimize(2, 3, "Ofast", "inline")
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
//#define int long long
//#define double long double
using i64 = long long;
using ui64 = unsigned long long;
//using i128 = __int128;
#define inf (int)0x3f3f3f3f3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define yn(x) cout << (x ? "yes" : "no") << endl
#define Yn(x) cout << (x ? "Yes" : "No") << endl
#define YN(x) cout << (x ? "YES" : "NO") << endl
#define mem(x, i) memset(x, i, sizeof(x))
#define cinarr(a, n) for (int _ = 1; _ <= n; _++) cin >> a[_]
#define cinstl(a) for (auto& _ : a) cin >> _
#define coutarr(a, n) for (int _ = 1; _ <= n; _++) cout << a[_] << " \n"[_ == n]
#define coutstl(a) for (const auto& _ : a) cout << _ << ' '; cout << endl
#define all(x) (x).begin(), (x).end()
#define ls (s << 1)
#define rs (s << 1 | 1)
#define ft first
#define se second
#define pii pair<int, int>
#ifdef DEBUG
    #include "debug.h"
#else
    #define dbg(...) void(0)
#endif

const int N = 2000 + 5;
//const int M = 1e5 + 5;
//const int mod = 998244353;
const int mod = 1e9 + 7;
//template <typename T> T ksm(T a, i64 b) { T ans = 1; for (; b; a = 1ll * a * a, b >>= 1) if (b & 1) ans = 1ll * ans * a; return ans; }
//template <typename T> T ksm(T a, i64 b, T m = mod) { T ans = 1; for (; b; a = 1ll * a * a % m, b >>= 1) if (b & 1) ans = 1ll * ans * a % m; return ans; }

int a[N];
int n, m, t, k, q;

template <int P> struct MInt {
    int x;
    MInt() : x(0) {}
    MInt(const signed& x) : x(norm(x % getMod())) {}
    MInt(const long long& x) : x(norm(x % getMod())) {}

    static int Mod;
    static int getMod() { return P > 0 ? P : Mod; }
    static void setMod(int _Mod) { Mod = _Mod; }
    int norm(const int& x) const { return x < 0 ? x + getMod() : (x >= getMod() ? x - getMod() : x); }
    int val() const { return x; }
    explicit operator int() const { return x; }
    MInt operator - () const { MInt res; res.x = norm(getMod() - x); return res; }
    MInt ksm(const long long& x) const {
        MInt a = *this, ans = 1;
        for (long long pw = x; pw; a *= a, pw >>= 1) if (pw & 1) ans *= a;
        return ans;
    }
    MInt inv() const { assert(x != 0); return ksm(getMod() - 2); }
    MInt& operator += (const MInt& b) & { x = norm(x + b.x); return *this; }
    MInt& operator -= (const MInt& b) & { x = norm(x - b.x); return *this; }
    MInt& operator *= (const MInt& b) & { x = norm(1ll * x * b.x % getMod()); return *this; }
    MInt& operator /= (const MInt& b) & { return *this *= b.inv(); }
    MInt& operator ++ () & { return *this += 1; }
    MInt& operator -- () & { return *this -= 1; }
    const MInt operator ++ (signed) { MInt old = *this; ++(*this); return old; }
    const MInt operator -- (signed) { MInt old = *this; --(*this); return old; }
    friend MInt operator + (const MInt& a, const MInt& b) { MInt res = a; res += b; return res; }
    friend MInt operator - (const MInt& a, const MInt& b) { MInt res = a; res -= b; return res; }
    friend MInt operator * (const MInt& a, const MInt& b) { MInt res = a; res *= b; return res; }
    friend MInt operator / (const MInt& a, const MInt& b) { MInt res = a; res /= b; return res; }
    friend istream& operator >> (istream& in, MInt& a) { long long v; in >> v; a = MInt(v); return in; }
    friend ostream& operator << (ostream& out, const MInt& a) { return out << a.val(); }
    friend bool operator == (const MInt& a, const MInt& b) { return a.val() == b.val(); }
    friend bool operator != (const MInt& a, const MInt& b) { return a.val() != b.val(); }
    friend bool operator < (const MInt& a, const MInt& b) { return a.val() < b.val(); }
    friend bool operator > (const MInt& a, const MInt& b) { return a.val() > b.val(); }
    friend bool operator <= (const MInt& a, const MInt& b) { return a.val() <= b.val(); }
    friend bool operator >= (const MInt& a, const MInt& b) { return a.val() >= b.val(); }
};
using Mint = MInt<mod>;
template<> int MInt<0>::Mod = 1;

Mint fac[N], c[N][N], s[N][N], p[N][N];

void init() {
    fac[0] = 1, c[0][0] = s[0][0] = p[0][0] = 1;
    for (int i = 1; i <= 2000; i++) {
        fac[i] = fac[i - 1] * i;
        c[i][0] = 1, s[i][0] = p[i][0] = 0;
        for (int j = 1; j <= i; j++) {
            c[i][j] = c[i - 1][j - 1] + c[i - 1][j];
            s[i][j] = s[i - 1][j - 1] + j * s[i - 1][j];
            p[i][j] = p[i - 1][j - 1] + p[i - j][j];
        }
    }
}

void work() {
    cin >> n >> k;
    t = 1;
    if (t == 1) {
        cout << Mint(k).ksm(n) << endl;
    }
    else if (t == 2) {
        cout << (n < m ? 0 : fac[n] / fac[n - m]) << endl;
    }
    else if (t == 3) {
        cout << fac[k] * s[n][k] << endl;
    }
    else if (t == 4) {
        cout << c[n + k - 1][k - 1] << endl;
    }
    else if (t == 5) {
        cout << c[k][n] << endl;
    }
    else if (t == 6) {
        cout << c[n - 1][k - 1] << endl;
    }
    else if (t == 7) {
        Mint ans = 0;
        for (int i = 1; i <= k; i++)
            ans += s[n][i];
        cout << ans << endl;
    }
    else if (t == 8) {
        cout << (k >= n) << endl;
    }
    else if (t == 9) {
        cout << s[n][k] << endl;
    }
    else if (t == 10) {
        cout << p[n + k][k] << endl;
    }
    else if (t == 11) {
        cout << (k >= n) << endl;
    }
    else {
        cout << p[n][k] << endl;
    }
}

signed main() {
#ifdef LOCAL
    freopen("C:\\Users\\admin\\CLionProjects\\Practice\\data.in", "r", stdin);
    freopen("C:\\Users\\admin\\CLionProjects\\Practice\\data.out", "w", stdout);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    init();
    int Case = 1;
    //cin >> Case;
    while (Case--) work();
    return 0;
}
/*
     _____   _   _       _  __    __
    |  _  \ | | | |     | | \ \  / /
    | |_| | | | | |     | |  \ \/ /
    |  ___/ | | | |  _  | |   }  {
    | |     | |_| | | |_| |  / /\ \
    |_|     \_____/ \_____/ /_/  \_\
*/
accuracy: 1
time_ms: 20
memory_kb: 50580
final_rank: 5

code_snippet: #include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
using ll = long long;

ll mod_pow(ll x, ll n, ll mod) {
    if (n == 0) {
        return 1;
    }
    ll
 res = mod_pow(x * x % mod, n / 2, mod);
    if (n & 1) res = res * x % mod;
    return res;
}

const ll MOD = 1e9 + 7;
const ll MAX = 1100000;

vector<ll> fact(MAX), inv(MAX), inv_fact(MAX);

void init() {
    fact.at(0) = fact.at(1) = 1;
    inv.at(0) = inv.at(1) = 1;
    inv_fact.at(0) = inv_fact.at(1) = 1;
    
    for (int i = 2; i < MAX; i++) {
        fact.at(i) = fact.at(i - 1) * i % MOD;
        inv.at(i) = MOD - inv.at(MOD % i) * (MOD / i) % MOD;
        inv_fact.at(i) = inv_fact.at(i - 1) * inv.at(i) % MOD;
    }
}

ll nCk(ll n, ll k) {
    ll x = fact.at(n);
    ll y = inv_fact.at(n - k);
    ll z = inv_fact.at(k);

    if (n < k) {
        return 0;
    }
    if (n < 0 || k < 0) {
        return 0;
    }

    return (x * ((y * z) % MOD)) % MOD;
}

int main() {
	init();
	ll n, k;
	cin >> n >> k;

	cout << mod_pow(k, n, MOD) << endl;
	
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 28688
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;

struct countcase {
	vector<long long> fac, finv, inv;
	int mod;

	countcase(int _mod = 1000000007, int n = 3100000) : mod(_mod), fac(n), finv(n), inv(n) {
		fac.resize(n);
		finv.resize(n);
		inv.resize(n);
		fac[0] = fac[1] = 1;
		finv[0] = finv[1] = 1;
		inv[1] = 1;
		for(int i = 2; i < n; i++) {
			fac[i] = fac[i - 1] * i % _mod;
			inv[i] = _mod - inv[_mod % i] * (_mod / i) % _mod;
			finv[i] = finv[i - 1] * inv[i] % _mod;
		}
	}

	long long c(int n, int k) {	 // 二項係数計算
		if(n < k) return 0;
		if(n < 0 || k < 0) return 0;
		return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
	}

	long long p(int n, int k) {	 //順列計算
		if(n < k) return 0;
		if(n < 0 || k < 0) return 0;
		return fac[n] * finv[n - k] % mod;
	}

	// nHk計算
	// n個の区別しないoを区別するk個の箱に入れる方法の総数
	//(n+k-1)C(k-1)と等しい
	long long h(int n, int k) { return c(n + k - 1, n); }

	long long pow(long long a, long long n) {
		a %= mod;
		if(a == 0) return 0LL;
		n %= mod - 1;
		long long ret = 1;
		while(n) {
			if(n & 1) (ret *= a) %= mod;
			(a *= a) %= mod;
			n >>= 1;
		}
		return ret;
	}
};

int main() {
	long long n, k;
	cin >> n >> k;
	countcase kazu(1000000007);
	cout << kazu.pow(k, n)<<endl;
}
accuracy: 1
time_ms: 210
memory_kb: 75444
final_rank: 6

code_snippet: #include "bits/stdc++.h"
#pragma GCC optimize("Ofast")

// Begin Header {{{
using namespace std;

#ifndef DEBUG
#define dump(...)
#endif

#define all(x) x.begin(), x.end()
#define rep(i, b, e) for (intmax_t i = (b), i##_limit = (e); i < i##_limit; ++i)
#define reps(i, b, e) for (intmax_t i = (b), i##_limit = (e); i <= i##_limit; ++i)
#define repr(i, b, e) for (intmax_t i = (b), i##_limit = (e); i >= i##_limit; --i)
#define var(Type, ...) Type __VA_ARGS__; input(__VA_ARGS__)

constexpr size_t    operator""_zu(unsigned long long value) { return value; };
constexpr intmax_t  operator""_jd(unsigned long long value) { return value; };
constexpr uintmax_t operator""_ju(unsigned long long value) { return value; };

constexpr int INF = 0x3f3f3f3f;
constexpr intmax_t LINF = 0x3f3f3f3f3f3f3f3f_jd;

template <class T, class Compare = less<>>
using MaxHeap = priority_queue<T, vector<T>, Compare>;
template <class T, class Compare = greater<>>
using MinHeap = priority_queue<T, vector<T>, Compare>;

inline void input() {}
template <class Head, class... Tail>
inline void input(Head&& head, Tail&&... tail) {
    cin >> head;
    input(forward<Tail>(tail)...);
}

template <class T>
inline istream& operator>>(istream &is, vector<T> &vec) {
    for (auto &e: vec) {
        is >> e;
    }
    return is;
}

inline void output() { cout << "\n"; }
template <class Head, class... Tail>
inline void output(Head&& head, Tail&&... tail) {
    cout << head;
    if (sizeof...(tail)) {
        cout << " ";
    }
    output(forward<Tail>(tail)...);
}

template <class T>
inline ostream& operator<<(ostream &os, const vector<T> &vec) {
    static constexpr const char *delim[] = {" ", ""};
    for (const auto &e: vec) {
        os << e << delim[&e == &vec.back()];
    }
    return os;
}

template <class T>
inline vector<T> makeVector(const T &initValue, size_t sz) {
    return vector<T>(sz, initValue);
}

template <class T, class... Args>
inline auto makeVector(const T &initValue, size_t sz, Args... args) {
    return vector<decltype(makeVector<T>(initValue, args...))>(sz, makeVector<T>(initValue, args...));
}

template <class Func>
class FixPoint : Func {
public:
    explicit constexpr FixPoint(Func&& f) noexcept : Func(forward<Func>(f)) {}

    template <class... Args>
    constexpr decltype(auto) operator()(Args&&... args) const {
        return Func::operator()(*this, std::forward<Args>(args)...);
    }
};

template <class Func>
static inline constexpr decltype(auto) makeFixPoint(Func&& f) noexcept {
    return FixPoint<Func>{forward<Func>(f)};
}

template <class Container>
struct reverse_t {
    Container &c;
    reverse_t(Container &c) : c(c) {}
    auto begin() { return c.rbegin(); }
    auto end() { return c.rend(); }
};

template <class Container>
auto reversed(Container &c) {
    return reverse_t<Container>(c);
}

template <class T>
inline bool chmax(T &a, const T &b) noexcept {
    return b > a && (a = b, true);
}

template <class T>
inline bool chmin(T &a, const T &b) noexcept {
    return b < a && (a = b, true);
}

template <class T>
inline T diff(const T &a, const T &b) noexcept {
    return a < b ? b - a : a - b;
}
// End Header }}}

const intmax_t MOD = intmax_t(1e9) + 7;
// ModInt {{{
template <intmax_t MOD>
class ModInt {
    intmax_t value;

public:
    inline ModInt(const ModInt& other) :
        value(other.value)
    {}

    inline ModInt(intmax_t value = 0) {
        if (value >= MOD) {
            this->value = value % MOD;
        } else if (value < 0) {
            this->value = (value + MOD) % MOD;
        } else {
            this->value = value;
        }
    }

    template <class T>
        explicit inline operator T() const {
            return static_cast<T>(value);
        }

    inline ModInt inverse() const {
        return ModInt::pow(value, MOD - 2);
    }

    inline ModInt& operator+=(const ModInt other) {
        value = (value + other.value) % MOD;
        return *this;
    }

    inline ModInt& operator-=(const ModInt other) {
        value = (MOD + value - other.value) % MOD;
        return *this;
    }

    inline ModInt& operator*=(const ModInt other) {
        value = (value * other.value) % MOD;
        return *this;
    }

    inline ModInt& operator/=(const ModInt other) {
        value = (value * other.inverse().value) % MOD;
        return *this;
    }

    inline ModInt operator+(const ModInt other) {
        return ModInt(*this) += other;
    }

    inline ModInt operator-(const ModInt other) {
        return ModInt(*this) -= other;
    }

    inline ModInt operator*(const ModInt other) {
        return ModInt(*this) *= other;
    }

    inline ModInt operator/(const ModInt other) {
        return ModInt(*this) /= other;
    }

    inline bool operator==(const ModInt other) {
        return value == other.value;
    }

    inline bool operator!=(const ModInt other) {
        return value != other.value;
    }

    friend ostream& operator<<(ostream &os, const ModInt other) {
        os << other.value;
        return os;
    }

    friend istream& operator>>(istream &is, ModInt& other) {
        is >> other.value;
        return is;
    }

    static constexpr inline ModInt pow(intmax_t n, intmax_t p) {
        intmax_t ret = 1;
        for (; p > 0; p >>= 1) {
            if (p & 1) ret = (ret * n) % MOD;
            n = (n * n) % MOD;
        }
        return ret;
    }
};

using Mint = ModInt<MOD>;
// }}}

// Factorial {{{
#ifdef DEBUG
#define constexpr
#endif

template <size_t N, size_t MOD>
struct Factorial {
    vector<uint_fast64_t> fact_;

    constexpr inline Factorial() : fact_(N + 1) {
        fact_[0] = 1;
        for (intmax_t i = 1; i <= N; ++i) {
            fact_[i] = (fact_[i - 1] * i) % MOD;
        }
    }

    constexpr inline Mint operator[](size_t pos) const {
        return fact_[pos];
    }
};

template <size_t N, size_t MOD>
struct InvFact {
    vector<uint_fast64_t> inv_;
    vector<uint_fast64_t> finv_;

    constexpr inline InvFact() : inv_(N + 1), finv_(N + 1) {
        inv_[1] = 1;
        finv_[0] = finv_[1] = 1;
        for (intmax_t i = 2; i <= N; ++i) {
            inv_[i] = (MOD - MOD / i) * inv_[MOD % i] % MOD;
            finv_[i] = finv_[i - 1] * inv_[i] % MOD;
        }
    }

    constexpr inline Mint operator[](size_t pos) const {
        return finv_[pos];
    }
};

#ifdef constexpr
#undef constexpr
#endif

Factorial<510000, MOD> fact;
InvFact<510000, MOD> finv;

inline Mint nCr(int n, int r) {
    if (r < 0 || n < r) {
        return 0;
    }
    return fact[n] * (finv[r] * finv[n - r]);
}

inline Mint nPr(int n, int r) {
    if (r < 0 || n < r) {
        return 0;
    }
    return fact[n] * finv[n - r];
}

inline Mint nHr(int n, int r) {
    return nCr(n + r - 1, r);
}
// }}}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.setf(ios_base::fixed);
    cout.precision(10);

    var(intmax_t, n, k);
    output(Mint::pow(k, n));

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 14536
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
#define rep(i,n) for (ll i=0;i<(n);++i)
#define rep2(i,a,b) for (ll i=(a);i<(b);++i)
#define debug(x) cout << #x << '=' << x << endl
#define all(v) (v).begin(),(v).end()
const ll MOD=1e9+7;
//const ll MOD=998244353;
const ll INF=1e9;
const ll IINF=1e18;
const double EPS=1e-8;
const double pi=acos(-1);

template<class T> inline bool chmin(T &a,T b){
    if (a>b){a=b; return true;} return false;
}
template<class T> inline bool chmax(T &a,T b){
    if (a<b){a=b; return true;} return false;
}

template<uint_fast64_t Modulus> class modint{
    using u64=uint_fast64_t;
    public:
    u64 a;
    constexpr modint(const u64 x=0) noexcept:a(((x%Modulus)+Modulus)%Modulus){}
    constexpr u64 &value() noexcept{return a;}
    constexpr const u64 &value() const noexcept{return a;}
    constexpr modint &operator+=(const modint &rhs) noexcept{
        a+=rhs.a;
        if (a>=Modulus) a-=Modulus;
        return *this;
    }
    constexpr modint operator+(const modint &rhs) const noexcept{
        return modint(*this)+=rhs;
    }
    constexpr modint &operator++() noexcept{
        return ++a,*this;
    }
    constexpr modint operator++(int) noexcept{
        modint t=*this; return ++a,t;
    }
    constexpr modint &operator-=(const modint &rhs) noexcept{
        if (a<rhs.a) a+=Modulus;
        a-=rhs.a;
        return *this;
    }
    constexpr modint operator-(const modint &rhs) const noexcept{
        return modint(*this)-=rhs;
    }
    constexpr modint &operator--() noexcept{
        return --a,*this;
    }
    constexpr modint operator--(int) noexcept{
        modint t=*this; return --a,t;
    }
    constexpr modint &operator*=(const modint &rhs) noexcept{
        a=a*rhs.a%Modulus;
        return *this;
    }
    constexpr modint operator*(const modint &rhs) const noexcept{
        return modint(*this)*=rhs;
    }
    constexpr modint &operator/=(modint rhs) noexcept{
        u64 exp=Modulus-2;
        while(exp){
            if (exp&1) *this*=rhs;
            rhs*=rhs; exp>>=1;
        }
        return *this;
    }
    constexpr modint operator/(const modint &rhs) const noexcept{
        return modint(*this)/=rhs;
    }
    constexpr modint operator-() const noexcept{
        return modint(Modulus-a);
    }
    constexpr bool operator==(const modint &rhs) const noexcept{
        return a==rhs.a;
    }
    constexpr bool operator!=(const modint &rhs) const noexcept{
        return a!=rhs.a;
    }
    constexpr bool operator!() const noexcept{return !a;}
    friend constexpr modint pow(modint &rhs,long long exp) noexcept{
        modint res{1};
        while(exp){
            if (exp&1) res*=rhs;
            rhs*=rhs; exp>>=1;
        }
        return res;
    }
    friend ostream &operator<<(ostream &s,const modint &rhs) noexcept {
        return s << rhs.a;
    }
    friend istream &operator>>(istream &s,modint &rhs) noexcept {
        u64 a; rhs=modint{(s >> a,a)}; return s;
    }
};

using mint=modint<MOD>;

const int MAX=5e5+10;
vector<mint> fac(MAX),finv(MAX),inv(MAX);
void COMinit(){
    fac[0]=fac[1]=1;
    finv[0]=finv[1]=1;
    inv[1]=1;
    rep2(i,2,MAX){
        fac[i]=fac[i-1]*i;
        inv[i]=-inv[MOD%i]*(MOD/i);
        finv[i]=finv[i-1]*inv[i];
    }
}
mint COM(int n,int k){
    if (n<k||n<0||k<0) return 0;
    return fac[n]*finv[k]*finv[n-k];
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    COMinit();
    ll n; mint k; cin >> n >> k;
    cout << pow(k,n) << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 14292
final_rank: 1

code_snippet: // DPL_5_A: Count the number of ways to put
// n distinguishable balls in
// k distinguishable boxes
//
// -> Sum over 1 <= j <= K of j! * S(n,j) * C(k, j)

#include <iostream>
using ll = long long;
ll S[1001][1001];
ll C[1001][1001];
ll M = 1e9 + 7;

int main() {
  int N, K;
  std::cin >> N >> K;

  S[0][0] = 1;
  for (int k = 1; k <= K; ++k)
    for (int n = k; n <= N; ++n)
      S[n][k] = (S[n - 1][k - 1] + k * S[n - 1][k]) % M;

  for (int n = 0; n <= K; ++n)
    C[n][0] = 1;
  for (int k = 1; k <= K; ++k)
    for (int n = k; n <= K; ++n)
      C[n][k] = (C[n - 1][k - 1] + C[n - 1][k]) % M;

  ll res = 0;
  ll F = 1;
  for (int j = 1; j <= K; ++j) {
    F = (F * j) % M;
    res = (res + ((((F * S[N][j]) % M) * C[K][j]) % M)) % M;
  }
  std::cout << res << "\n";
}
accuracy: 1
time_ms: 10
memory_kb: 16852
final_rank: 3