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. DAY5-模块与包

    一.模块的介绍 1.什么是模块 #常见的场景:一个模块就是一个包含了一组功能的python文件,比如spam.py,模块名为spam,可以通过import spam使用. #在python中,模块的使 ...

  2. Android常用开源库集合【持续更新】

    1.FastJson  阿里巴巴工程师做的一个方便的JSON转换库 2.ButterKnife 只要作用能代替代码中大量的findviewbyid语句的使用,使用@injectview注入方式 3.v ...

  3. Codeforces 1107E (Vasya and Binary String) (记忆化,DP + DP)

    题意:给你一个长度为n的01串,和一个数组a,你可以每次选择消除一段数字相同的01串,假设消除的长度为len,那么收益为a[len],问最大的收益是多少? 思路:前两天刚做了POJ 1390,和此题很 ...

  4. PDF的一些工具

    PdfStreamDumper Pdftk 这里有一个链接,列出了很多pdf免费工具 https://en.wikipedia.org/wiki/Category:Free_PDF_software ...

  5. Luogu 3457 [POI2007]POW-The Flood

    感觉自己什么题都写不动了. 又是一个神贪心:把所有城市中的点按照高度从小到大排序之后拿出来逐个计算,枚举其他高度小于它的点向四周扩展,如果这个点不能被之前放过的抽水机覆盖,那么把答案加一,并在这个点放 ...

  6. Git 之 github分享代码

    作为一个技术人员还是脱离不了屌丝的本质,所以每天都是逛逛github,看看别人有什么好的项目,自己可以给他挑挑bug也可以提供自己的水平,但是别人不那怎么才能给别人贡献代码呢?那就是fork了.... ...

  7. js获取指定小时日期格式化

    不得不感叹一下,聪明的程序员写的代码真是让人惊奇 找了一圈格式化代码的方式,下面的这个使用了一个 slice 函数,真是厉害 https://stackoverflow.com/questions/4 ...

  8. xml知识点

    XML 被设计用来传输和存储数据. HTML 被设计用来显示数据.应该掌握的基础知识:在您继续学习之前,需要对以下知识有基本的了解: HTML / XHTML JavaScript 如果您希望首先学习 ...

  9. Dapper --Execute

    Dapper-Execute Ececute是一种可被任何IDbConnection类型的对象调用的扩展方法.它可以执行一次或多次命令, 并返回受影响的行数.此方法通常用于执行存储过程.插入.更新.删 ...

  10. SQL:bat批处理多个.sql文件

    一.批处理文件 update.bat       设置sqlserver的实例     记得一定要配置logfile文件 @echo off :: #### B. #### set LOGFILE=& ...