题目:Cut Pieces
题意:对于方块,我们可以有 ai 种填色方案,给定方案中,连续的视为一段,求 ai 的某一个排列时,方案中段数和最大的情况。
思路:看标程比较好理解
用通俗的话说就是,总的方案数是n*S (S = a1*a2*...*an) ,对于其间重复计算的价值就是相邻时颜色一样的,得去掉,而总的重复的部分,就是S/max(v[i],v[i+1]) 的和,关于取的数列为什么是一大一小,上面已经解释的很清楚了;由于数值比较大,而取模数恰好是个素数,所以逆元就不需要用扩展欧几里德求了,后面的都很好处理,直接看代码吧。
#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
#define maxn 1000010
typedef __int64 LL;
vector<LL>v;
const LL mod = 1e9+7;
LL num[maxn];
LL Pow(LL a,LL b)
{
LL ans=1;
while(b)
{
if(b&1)
{
b--;
ans=(ans*a)%mod;
}
else
{
b/=2;
a=(a*a)%mod;
}
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
v.clear();
int n;
scanf("%d",&n);
LL S=1;
for(int i=1;i<=n;i++)
{
scanf("%I64d",&num[i]);
S=(S*num[i])%mod;
}
sort(num+1,num+n+1);
int l=1,r=n;
while(l<=r)
{
if(l==r)
v.push_back(num[l]);
else
{
v.push_back(num[l]);
v.push_back(num[r]);
}
l++;
r--;
}
LL ans=0;
for(int i=0;i<n-1;i++)
{
//cout<<"v["<<i<<"]="<<v[i]<<endl;
ans+=S*Pow(max(v[i],v[i+1]),mod-2);
ans%=mod;
}
ans=S*n-ans;
ans=(ans%mod+mod)%mod;
printf("%I64d\n",ans);
}
return 0;
}