#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
typedef long long ll;
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
ll mod = 1e9 + 7;
ll qp(ll a, ll b=mod-2) {
if(b==0) return 1;
if(b==1) return a%mod;
if(b%2) return (a * qp(a,b-1))%mod;
ll x = qp(a,b/2);
return (x*x)%mod;
}
ll odwr, k, m, n;
ll ans = 0;
void rek(vector<ll> p, ll chance, ll len=1) {
ll mn = p[0];
for(ll i=0; i<n; ++i) if(p[i] < mn) mn = p[i];
for(ll i=0; i<n; ++i) {
if(p[i]==mn) {
for(ll c=1; c<=k; ++c) {
if(p[i]+c >= m) ans = (ans + chance * len)%mod;
else {
p[i]+=c;
rek(p,(chance * odwr)%mod, len+1);
p[i] -= c;
}
}
return;
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> k >> m;
odwr = qp(k);
vector<ll> p(n,0);
rek(p,odwr);
cout << ans << "\n";
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> typedef long long ll; using namespace __gnu_pbds; using namespace std; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; ll mod = 1e9 + 7; ll qp(ll a, ll b=mod-2) { if(b==0) return 1; if(b==1) return a%mod; if(b%2) return (a * qp(a,b-1))%mod; ll x = qp(a,b/2); return (x*x)%mod; } ll odwr, k, m, n; ll ans = 0; void rek(vector<ll> p, ll chance, ll len=1) { ll mn = p[0]; for(ll i=0; i<n; ++i) if(p[i] < mn) mn = p[i]; for(ll i=0; i<n; ++i) { if(p[i]==mn) { for(ll c=1; c<=k; ++c) { if(p[i]+c >= m) ans = (ans + chance * len)%mod; else { p[i]+=c; rek(p,(chance * odwr)%mod, len+1); p[i] -= c; } } return; } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k >> m; odwr = qp(k); vector<ll> p(n,0); rek(p,odwr); cout << ans << "\n"; return 0; } |
English