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

Output

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

Example
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,
as351 = 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<queue>
#include<stack>
#include<vector>
#include<math.h>
#include<stdio.h>
#include<numeric>//STL数值算法头文件
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<functional>//模板类头文件
using namespace std; const long long INF=1e9+7;
const long long maxn=101000; long long n,x;
long long a[maxn]; long long quick_mod(long long a,long long b)
{
long long ans=1;
a=a%INF;
while(b)
{
if(b&1)
ans=ans*a%INF;
a=a*a%INF;
b>>=1;
}
return ans;
} int main()
{
while(~scanf("%d %d",&n,&x))
{
long long sum1=0;
for(long long i=0; i<n; i++)
{
scanf("%d",&a[i]);
sum1+=a[i];
}
for(long long i=0; i<n; i++)
a[i]=sum1-a[i];
sort(a,a+n);
long long ans,j=1,cot=1,t;
for(j=1; j<=n; j++)
{
if(a[j]!=a[j-1])
{
if(cot%x)
{
ans=a[j-1];
break;
}
else
{
cot/=x;
a[j-1]+=1;
j--;
}
}
else cot++;
}
printf("%d\n",quick_mod(x,min(ans,sum1)));
}
return 0;
} #include<queue>
#include<stack>
#include<vector>
#include<math.h>
#include<stdio.h>
#include<numeric>//STL数值算法头文件
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<functional>//模板类头文件
using namespace std; const long long INF=1e9+7;
const long long maxn=101000; long long n,x;
long long a[maxn]; long long gcd(long long a,long long b)
{
long long ans=1;
ans=ans%INF;
while(b)
{
if(b&1)
ans=ans*a%INF;
a=a*a%INF;
b>>=1;
}
return ans;
} int main()
{
while(~scanf("%I64d %I64d",&n,&x))
{
long long sum1=0;
for(long long i=0; i<n; i++)
{
scanf("%I64d",&a[i]);
sum1+=a[i];
}
for(long long i=0; i<n; i++)
a[i]=sum1-a[i];
sort(a,a+n);
long long ans,j=1,cot=1,t;
while(j<=n)
{
if(a[j]!=a[j-1])
{
if(cot%x)
{
ans=a[j-1];
break;
}
long long f=a[j-1]+1;
t=cot/x;
for(long long k=j-1,s=t; s>0; s--,k--)
a[k]=f;
j-=t;
j++;
cot=1;
}
else cot++,j++;
}
printf("%I64d\n",gcd(x,min(ans,sum1)));
}
return 0;
}

Prime Number CodeForces - 359C (属于是数论)的更多相关文章

  1. CodeForce 359C Prime Number

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

  2. Relatively Prime Powers CodeForces - 1036F (莫比乌斯函数容斥)

    Relatively Prime Powers CodeForces - 1036F Consider some positive integer xx. Its prime factorizatio ...

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

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

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

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

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

  6. 10 001st prime number

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

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

  8. [Swift]LeetCode762. 二进制表示中质数个计算置位 | 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 ...

  9. 10_ for 练习 _ is Prime Number ?

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

随机推荐

  1. 天梯赛 L2-006 树的遍历 (二叉树)

    给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列.这里假设键值都是互不相等的正整数. 输入格式: 输入第一行给出一个正整数N(<=30),是二叉树中结点的个数.第二行给出其后序遍历序 ...

  2. linux 在命令行中通过conda使用anaconda

    在 ~/.bash_profile中添加 export PATH="/home/taoke/anaconda/bin:$PATH"

  3. localhost或127.0.0.1或192.168.1.*被转到129129.com上的问题

    系统启动里会有个httpd的apache程序在运行,自启禁用掉后.windows下有个apache文件夹,干掉就可以. 个别GHOST XP程序里面会装这种流氓程序.

  4. sicily 1259. Sum of Consecutive Primes

    Description Some positive integers can be represented by a sum of one or more consecutive prime numb ...

  5. 在Ubuntu上使用pip安装错误 read timed out 处理方法

    在终端输入 pip --default-timeout=1000 install -U pip 也就是修改超时时间.

  6. 出现ERROR: While executing gem ... (Gem::FilePermissionError)这种错误的解决办法

    重新安装ruby即可解决 brew install ruby

  7. 9.Python3标准库--数据压缩与归档

    ''' 尽管现代计算机系统的存储能力日益增长,但生成数据的增长是永无休止的. 无损(lossless)压缩算法以压缩或解压缩数据花费的时间来换取存储数据所需要的空间,以弥补存储能力的不足. Pytho ...

  8. 转:google测试分享-GTA

    原文: http://blog.sina.com.cn/s/blog_6cf812be0102viuh.html 上一次分享了google测试分享-分层测试,有很多自动化测试的策略和实施都要有一个重点 ...

  9. Java web项目中新建maven项目出现的问题

    1.首先新建maven项目,新建Maven时出现了版本问题,报错 第一个错误:jdk版本与project facets不匹配(大概是这样,忘记截图了),那么解决办法是: 在项目右击--->Pro ...

  10. Vue进阶篇

    前引 今天是2018年12月30,虽不是2018年的最后一天,但是却是自己在2018年写的最后一篇博客了,昨天下班在地铁上闲来无事,翻起了关注的一些公众号发的技术博文,里面就提到写博客的重要性,其实这 ...