题目链接
题目:
在1-n之间随机生成长度为n的整数序列,请问正好含有n-1个不同的整数的方案数,答案mod 1e9+7。
思路:
首先从n个数中拿n-1个会有n种情况,再从n-1个中选出一个添加会有n-1种情况,再将这n个仅有一个重复的数字排列一遍就会有n!/2种情况,所以答案就是n!/2*(n-1)*n,过程中取模。
代码:
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int N=1e5+5;
const int mod=1e9+7;
const int inf=0x7fffffff;
const double pi=3.1415926535;
using namespace std;
signed main()
{
IOS;
int n,arr[N];
arr[1]=1;
arr[2]=1;
for(int i=3;i<=N-1;i++)
{
arr[i]=(arr[i-1]*i)%mod;
}
while(cin>>n)
{
cout<<(arr[n]*((n*(n-1))%mod))%mod<<endl;
}
return 0;
}