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. JAVA多线程提高九:Semaphere同步工具

    java 中Semaphere可类比操作系统信号量,硬件资源如IO.内存.磁盘等都是有固定量的,多个程序需要竞争这些资源,没有资源就需要被挂起. 一.类和方法摘要 构造函数: public Semap ...

  2. 51nod1471 小S的兴趣

    题目来源: CodeForces 基准时间限制:1.5 秒 空间限制:131072 KB 分值: 320 小S喜欢有趣的事.但是,每个人的兴趣都是独特的.小S热衷于自问自答.有一天,小S想出了一个问题 ...

  3. echarts初探:了解模块化

    什么是echarts?这是官网:http://echarts.baidu.com/ 简单的说就是百度提供的一些画图表的库,用它你可以简便的画出一些你想要的图表效果. 虽然蛮好用的,但对于不知道模块化的 ...

  4. 天梯赛 L2-012 关于堆的判断 (二叉树)

    将一系列给定数字顺序插入一个初始为空的小顶堆H[].随后判断一系列相关命题是否为真.命题分下列几种: "x is the root":x是根结点: "x and y ar ...

  5. python3学习笔记.1.初体验

    最近工作烦得很 就想找点儿别的事情来做,于是想到了学学python. 因为是vs2017,所以就在里面安装了. 第一个程序肯定是Hello World了. 新建一个python应用程序 代码只有一行 ...

  6. Vuex-Action

    Action 类似于 mutation,不同在于: Action 提交的是 mutation,而不是直接变更状态. Action 可以包含任意异步操作. 让我们来注册一个简单的 action: con ...

  7. Linux内核中实现生产者与消费者(避免无效唤醒)【转】

    转自:http://blog.csdn.net/crazycoder8848/article/details/42581399 本文关注的重点是,避免内核线程的无效唤醒,并且主要是关注消费者线程的设计 ...

  8. [ python ] 练习作业 - 2

    1.写函数,检查获取传入列表或元组对象的所有奇数位索引对应的元素,并将其作为新列表返回给调用者. lic = [0, 1, 2, 3, 4, 5] def func(l): return l[1::2 ...

  9. PlantUML——1.Hello

    官网: http://www.plantuml.com/ 第一步: 下载 plantuml.jar文件: 第二步:创建一个demo.txt文件(与plantuml.jar在同一目录),内容如下: @s ...

  10. LightOJ 1319 Monkey Tradition(中国剩余定理)

    题目链接:https://vjudge.net/contest/28079#problem/U 题目大意:给你n(n<12)行,每行有pi,ri,求一个数ans满足ans%pi=ri(i从1~n ...