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).

The first line contains two positive integers n and x (1 ≤ n ≤ 105, 2 ≤ 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).

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

Examples

Input
2 2
2 2
Output
8
Input
3 3
1 2 3
Output
27
Input
2 2
29 29
Output
73741817
Input
4 5
0 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·27, 729 = 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.

思路是主要的进制的思想

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath>
#define mod 1000000007 const int maxn=1e5+;
typedef long long ll;
using namespace std;
ll quickpow(ll a,ll b)
{
ll ans=;
while(b)
{
if(b&)
ans=(ans*a)%mod;
b>>=;
a=(a*a)%mod;
}
return ans;
}
ll a[maxn];
int main()
{
ll n,x;
cin>>n>>x;
ll s=;
for(int t=;t<n;t++)
{
scanf("%lld",&a[t]);
s+=a[t];
}
for(int t=;t<n;t++)
{
a[t]=s-a[t];
}
ll ans,cnt=;
sort(a,a+n);
a[n]=-;
for(int t=;t<=n;t++)
{
if(a[t]!=a[t-])
{
if(cnt%x)
{
ans=a[t-];
break;
}
else
{
cnt/=x;
a[t-]++;
t--;
}
}
else
{
cnt++;
}
}
ll ss=min(s,ans);
cout<<quickpow(x,ss)<<endl;
return ;
}

CodeForces - 359C-Prime Number的更多相关文章

  1. CodeForce 359C Prime Number

    Prime Number CodeForces - 359C Simon has a prime number x and an array of non-negative integers a1,  ...

  2. Prime Number CodeForces - 359C (属于是数论)

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

  3. Codeforces H. Prime Gift(折半枚举二分)

    题目描述: Prime Gift time limit per test 3.5 seconds memory limit per test 256 megabytes input standard ...

  4. FZU 1649 Prime number or not米勒拉宾大素数判定方法。

    C - Prime number or not Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

  5. 每日一九度之 题目1040:Prime Number

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6732 解决:2738 题目描述: Output the k-th prime number. 输入: k≤10000 输出: The k- ...

  6. LintCode-Kth Prime Number.

    Design an algorithm to find the kth number such that the only prime factors are 3, 5, and 7. The eli ...

  7. Codeforces 55D Beautiful Number

    Codeforces 55D Beautiful Number a positive integer number is beautiful if and only if it is divisibl ...

  8. 10 001st prime number

    这真是一个耗CPU的运算,怪不得现在因式分解和素数查找现在都用于加密运算. By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13 ...

  9. CodeForces 432C Prime Swaps

    Description You have an array a[1], a[2], ..., a[n], containing distinct integers from 1 to n. Your ...

  10. [LeetCode] Prime Number of Set Bits in Binary Representation 二进制表示中的非零位个数为质数

    Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...

随机推荐

  1. opencv中文网站相关下载

    http://wiki.opencv.org.cn/index.php/Download

  2. python爬虫(7)--Beautiful Soup的用法

    1.Beautiful Soup简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据. Beautiful Soup提供一些简单的.python式的函数用来 ...

  3. Opencv中Rect_类

    Rect_类有些意思,成员变量x.y.width.height,分别为左上角点的坐标和矩形的宽和高.常用的成员函数有Size()返回值为一个Size,area()返回矩形的面积,contains(Po ...

  4. (转)typedef和#define的用法与区别

    typedef和#define的用法与区别 一.typedef的用法 在C/C++语言中,typedef常用来定义一个标识符及关键字的别名,它是语言编译过程的一部分,但它并不实际分配内存空间,实例像: ...

  5. C语言访问网页

    一.理论 http://www.zixue7.com/thread-3860-1-1.html

  6. 20、Basic Shell_for_while_grep_find

    转载:https://github.com/swcarpentry/DEPRECATED-boot-camps/blob/master/shell/shell_cheatsheet.md 1.For ...

  7. NSClassFromString 实例话静态库中的类

    Class myClass = NSClassFromString("StaticLibyClassName"); StaticLibyClassName是从静态库中实例化一个Cl ...

  8. Dojo Javascript 编程规范(转)

    前言 相当不错的 Javascript 编程风格规范,建议大家采用此规范编写 Javascript.原文链接: http://dojotoolkit.org/developer/StyleGuide ...

  9. c# dictionary,list排序

    Dictionary Key排序 Dictionary<string, string> dct= new Dictionary<string, string>(); Dicti ...

  10. 在OnRowDataBound或OnItemDataBound事件中获取字段值

    不管是在GridView,DataList还是Repeater控件中,其中Repeater控件,没有DataKeyNames或是DataKeyField属性,想获取记录的主键值,只好用Label或是H ...