Question meaning: n white balls. n-1 machines, the i-th machine will turn the (i, i+1)-th ball into black.
An operation sequence is [1..n-1] permutation, its value Defined as the time when n balls turn black for the first time.
2<=n<=1e6. Given n, ask all permutations of operations, the sum of their values modulo 1e9+7 ?
First, the two machines 1, n-1 must be selected, otherwise 1, n will not become black .Assume
1=a[1]<a[2]<.....a[k] =n-1 Then the interval of a[i]-a[i-1] is 1 or 2. Let these two The number is a, b.
You can get a+b=k-1 , a+2b=n-2 to solve b=nk-1 Then choose b from k-1 intervals as interval 2, and you can determine a plan .The number of methods is C(k-1,nk-1)
An operation sequence is [1..n-1] permutation, its value Defined as the time when n balls turn black for the first time.
2<=n<=1e6. Given n, ask all permutations of operations, the sum of their values modulo 1e9+7 ?
After reading the solution.. first film under rng_58.
How many permutations are there with the calculated value <= k? Then the value of k is d[k]-d[k-1]First, the two machines 1, n-1 must be selected, otherwise 1, n will not become black .Assume
1=a[1]<a[2]<.....a[k] =n-1 Then the interval of a[i]-a[i-1] is 1 or 2. Let these two The number is a, b.
You can get a+b=k-1 , a+2b=n-2 to solve b=nk-1 Then choose b from k-1 intervals as interval 2, and you can determine a plan .The number of methods is C(k-1,nk-1)
Then the total number of permutations of value k is: C(k-1,nk-1) * k! * (n-1-k) !.
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N=1e6+5,mod=1e9+7; ll n,f[N]; ll powmod(ll x,ll n) { ll s=1; while(n) { if(n&1) s=(s*x)%mod; n>>=1; x=(x*x)%mod; } return s; } ll C(ll n,ll m) { if(n<m) return 0; ll a=f[n],b=(f[n-m]*f[m])%mod; return (a*powmod(b,mod-2))%mod; } intmain() { cin>>n; f[0]=1; for(int i=1;i<=n;i++) f[i]=(f[i-1]*i)%mod; ll res=0,pre=0; for(int k=1;k<n;k++) { ll tk=(f[k]*f[n-1-k])%mod; tk = (tk * C (k-1, nk-1))% mod; ll num = (tk-pre + mod)% mod; res = (res + (num * k)% mod)% mod; pre = tk; } cout<<res<<'\n'; return 0; }
When counting, you can first assume order and calculate its plan. Then arrange