Codeforces-258C:Little Elephant and LCM(数论)

来源:互联网 发布:mac版qq接收文件 编辑:程序博客网 时间:2024/05/21 11:28

C. Little Elephant and LCM
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The Little Elephant loves the LCM (least common multiple) operation of a non-empty set of positive integers. The result of the LCM operation of k positive integers x1, x2, ..., xk is the minimum positive integer that is divisible by each of numbers xi.

Let's assume that there is a sequence of integers b1, b2, ..., bn. Let's denote their LCMs as lcm(b1, b2, ..., bn) and the maximum of them as max(b1, b2, ..., bn). The Little Elephant considers a sequence b good, if lcm(b1, b2, ..., bn) = max(b1, b2, ..., bn).

The Little Elephant has a sequence of integers a1, a2, ..., an. Help him find the number of good sequences of integers b1, b2, ..., bn, such that for all i (1 ≤ i ≤ n) the following condition fulfills: 1 ≤ bi ≤ ai. As the answer can be rather large, print the remainder from dividing it by 1000000007 (109 + 7).

Input

The first line contains a single positive integer n (1 ≤ n ≤ 105) — the number of integers in the sequence a. The second line contains nspace-separated integers a1, a2, ..., an (1 ≤ ai ≤ 105) — sequence a.

Output

In the single line print a single integer — the answer to the problem modulo 1000000007 (109 + 7).

Examples
input
41 4 3 2
output
15
input
26 3
output
13
思路:将数列a排序(改变a中元素排列顺序不改变答案),枚举最大值,那么b数列中其他的值一定是那个最大值的因子。那么找出其所有因子并排序。二分找出包含这个因子的a序列的区间。

如:a=[1, 2 ,3 ,4 ]                                                     4

枚举最大值                                                     2,2,2

首先是4,a数列中各数包含4的因子分别为 1,1,1,1 ,那么最大值为4时的b数列的方案数为1*2*2*(4-3)=4种。因为要满足最大值为4,所以最后只能取4.

                                                                            3,3

其次是3,a数列中各数包含3的因子分别为 1,1 ,1,1 ,那么最大值为3时的b数列的方案数为1*1*(2*2-1)=3种。因为要满足最大值为3,所以最后有3种取法。

以此类推。

#include<bits/stdc++.h>typedef __int64 ll;using namespace std;const int MOD=1e9+7;const int MAX_N = 1e6;int all=0,p[MAX_N],m;int pr[MAX_N+100],a[MAX_N];bool isp[MAX_N+10];void init(){all = 0;memset(isp,0,sizeof isp);isp[1]=1;for(int i=2;i<=MAX_N;i++){if(!isp[i])pr[all++] = i;for(int j=0;j<all;j++){ll t = (ll)1*pr[j]*i ;if(t<=MAX_N){isp[t] = true;if(i%pr[j]==0)break;}else break;}}}void dive(int x){    m=0;    for(int i=1;i*i<=x;i++)    {        if(x%i==0)        {            p[m++]=i;            if(x/i!=i)p[m++]=x/i;        }    }    sort(p+1,p+m);}ll POW(ll x,ll y){    ll res=1;    while(y)    {        if(y&1)res=res*x%MOD;        x=(x*x)%MOD;        y/=2;    }    return res%MOD;}int main(){    init();    int n;    scanf("%d",&n);    for(int i=0;i<n;i++)scanf("%d",&a[i]);    sort(a,a+n);    ll ans=0;    for(int i=a[n-1];i>=1;i--)    {        dive(i);        ll st=0,now=1;        for(int j=1;j<m;j++)        {            ll en=lower_bound(a,a+n,p[j])-a;            now=now*POW(j,en-st)%MOD;            st=en;        }        now=now*(((POW(m,n-st)-POW(m-1,n-st))%MOD+MOD)%MOD)%MOD;        ans=(ans+now)%MOD;    }    cout<<ans<<endl;}



原创粉丝点击