Codeforces Round #157 (Div. 1), problem: (C) Little Elephant and LCM DP+组合数学 0(n^(5/2))

题意:有一个数列a1....an,每个元素都可以自减形成一个新序列,问这样的数列(LCM(bn)=max(bn)),会有几个.

做法:最终形成的数列的元素相互没有关系,所有可以根据枚举max来确定最后的序列。

为了方便统计,就先对a排序。此时,在收集max的除子,然后在a中搜索比每个除子小的元素的个数,之后的工作就简单了

/******
先使序列变得有序,然后再考虑问题.
记得要看出这题数字的原始序列原本可以忽略的。
********/
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#define LL long long
#define mod 1000000007
using namespace std;
const int LMT=100005;
int a[LMT],n;
vector<int>elem;
LL pow(LL x,int num)
{
    if(num<0)return 0;
    LL res=1;
    while(num)
    {
        if(num&1)res*=x;
        res%=mod;
        x=x*x;
        x%=mod;
        num>>=1;
    }
    return res;
}
int myfind(int x)
{
    int res,mid,l=0,r=n-1;
    while(l<=r)
    {
        mid=(l+r)>>1;
        if(a[mid]<x)l=mid+1;
        else
        {
            res=mid;
            r=mid-1;
        }
    }
    return res;
}
int main(void)
{
    int pos,pre,m=0;
    LL ans=0,sum=0;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
      {
          scanf("%d",&a[i]);
          m=max(a[i],m);
      }
    sort(a,a+n);
    for(int i=1;i<=m;i++)
    {
        elem.clear();sum=1;pre=myfind(1);
        for(int j=1;j*j<=i;j++)
        if(i%j==0)
        {
            elem.push_back(j);
            if(j!=i/j)elem.push_back(i/j);
        }
        sort(elem.begin(),elem.end());
        int k;
        for( k=1;k<(int)elem.size();k++)
        {
            pos=myfind(elem[k]);
            sum*=pow(k,pos-pre);
            sum%=mod;
            pre=pos;
        }
        sum*=(pow(k,n-pre)-pow(k-1,n-pre)+mod)%mod;
        sum%=mod;
        ans+=sum;
        ans%=mod;
    }
    printf("%I64d\n",ans);
    return 0;
}


你可能感兴趣的:(Codeforces Round #157 (Div. 1), problem: (C) Little Elephant and LCM DP+组合数学 0(n^(5/2)))