CodeForces

来源:互联网 发布:win10软件卸载不了 编辑:程序博客网 时间:2024/05/29 19:36
题目:
C. Prime Number
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Simon has a prime number x and an array of non-negative integers a1, a2, …, an.

Simon loves fractions very much. Today he wrote out number  on a piece of paper. After Simon led all fractions to a common denominator and summed them up, he got a fraction: , where number t equals xa1 + a2 + … + an. Now Simon wants to reduce the resulting fraction.

Help him, find the greatest common divisor of numbers s and t. As GCD can be rather large, print it as a remainder after dividing it by number 1000000007 (109 + 7).

Input

The first line contains two positive integers n and x (1 ≤ n ≤ 1052 ≤ x ≤ 109) — the size of the array and the prime number.

The second line contains n space-separated integers a1, a2, …, an (0 ≤ a1 ≤ a2 ≤ … ≤ an ≤ 109).

Output

Print a single number — the answer to the problem modulo 1000000007 (109 + 7).

Examples
input
2 22 2
output
8
input
3 31 2 3
output
27
input
2 229 29
output
73741817
input
4 50 0 0 0
output
1
Note

In the first sample . Thus, the answer to the problem is 8.

In the second sample, . The answer to the problem is 27, as 351 = 13·27729 = 27·27.

In the third sample the answer to the problem is 1073741824 mod 1000000007 = 73741817.

In the fourth sample . Thus, the answer to the problem is 1.

思路

markdown写一次题解试试,在这道题中,上给了两个n(数字个数)x(一个素数,是下面各个数的底数),接下来有n个数,比如第二组样例:

3 31 2 3

结果是:

27

361+362+36331×32×33,6代表各个次幂的和,是由131+32+33推导而来,所以我们先算出sum,也就是各个次幂的和,然后在分子里面找出那个最小次幂的项,也就是33,但是在第一组样例中,出现了错误,在第一组样例中会变成这样22+22=2×22=23,对于这种情况分子求出来的次数是3而不是2,所以我们还要对分子的项进行处理。处理的方法是将相同次数的项加到一块,这里我用map处理,然后每次取次数最小的t,判断系数r是否能被x整除,如果不可以表示这个t就是我们要找的分子的t,否则就将r中一个x乘到xt上去,也就是将r/x加到xt+1的系数上去,直到找到一个最小的t并且r%x≠0,最后用快速幂得到答案

代码

#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <string>#include <iostream>#include <stack>#include <map>#include <queue>#include <vector>#include <algorithm>#define mem(a,b) memset(a,b,sizeof(a))#define N (100000+20)#define M 200010#define MOD (1000000000+7)#define inf 0x3f3f3f3f#define LL long longusing namespace std;LL a[100001];LL power(LL a,LL n){    LL s=1;    while (n)    {        if (n&1)            s=s*a%MOD;        a=a*a%MOD;        n>>=1;    }    return s;}int main(){    LL n;    LL x,sum=0;    scanf("%lld%lld",&n,&x);    for (LL i=1; i<=n; ++i)    {        scanf("%lld",&a[i]);        sum+=a[i];//算出分母的大小    }    LL ans=sum-a[n],cnt=0;    for (LL k=a[n]; k; k--)    {        while (n&&a[n]==k)        {            n--;            cnt++;//系数        }        if (cnt%x)            break;        ans++;//算出一共有几项        cnt/=x;    }    printf("%lld\n",power(x,ans));    return 0;}

顺便贴一个我之前暴力想过的代码,代码应该没错,不过一是long long可能存不下那么大的数,二是我知道要超时,果然在第五个样例的时候超时了:

#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <string>#include <iostream>#include <stack>#include <map>#include <queue>#include <vector>#include <algorithm>#define mem(a,b) memset(a,b,sizeof(a))#define N (100000+20)#define M 200010#define MOD (1000000000+7)#define inf 0x3f3f3f3f#define LL long longusing namespace std;LL a[N];LL cifang(LL x,LL y){    LL sum=1;    for(LL i=0; i<y; i++)    {        sum*=(x%MOD);    }    return (sum%MOD);}LL lcm(LL x,LL y){    return ((x*y)/(__gcd(x,y)))%MOD;}int main(){    LL n,x,l,f1=0,f2=1,maxx=0,f,num;    scanf("%lld%lld",&n,&x);    for(LL i=0; i<n; i++)    {        scanf("%lld",&l);        f=(cifang(x,l))%MOD;        a[i]=f;    }    for(LL i=0; i<n; i++)        f2*=(lcm(a[i],f2))%MOD;    for(LL i=0; i<n; i++)        f1+=(f2/a[i]);    num=__gcd(f1,f2);    printf("%lld\n",num%MOD);    return 0;}

参考博客:http://blog.csdn.net/hongrock/article/details/14163805

最后再说一下c语言中的define:
c语言#define中要注意,定义

#define MOD (1000000000+7)

的时候要注意加上括号,因为宏定义在替换的时候是直接替换,而并不把MOD的值计算出来,当你取模的时候,编译器会先进行取模运算,最后才把7加上,导致结果错误,所以要注意一下

0 0
原创粉丝点击