CodeForces - 359C

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

这道题还是蛮陌生的。 分母已经固定的为  x^s s= (a1+a2+a3+...),所以最主要的就是处理分子了,我们知道分子上k的最小值一定是相加时分母的最大值。

首先对于每个1/x^ai 通分之后分子变为x^(s-ai),我们的任务就是要求出分子中的最大公约数就是我们所求的值,肯定就是次数最小的那一项了;

但是有一个特殊情况我们需要考虑,就是有些项相加的系数可能会被x整除,所以我们还要对分子进行处理;

例如分子为2^2+2^2=2*2^2=2^3,最小的k为3,不是2

处理方法:

   我们可以知道的是,由于输入的ai是按照非递减的顺序输入,s=a1+a2+a3+...,所以初始最小的k应为 s-an;(即 1/x^an 分母变为 x^s 需要乘上x^s-an 为最小);

然后我们就需要开始找相同项的次数加在一块能否整除x;我们这里从最小项an开始,因为只有最小的项加起来的次数可以整除x才能改变最小的k值,最小的没有,最大的对k的值没有影响,)每次取最小的值t的系数,判断能否被x整除,如果不可以这个t就直接是我们要找的k,如果可以整除那么我们就乘一个x到x^t上去 使其变为x^(t+1),也就是将count/x的系数加到x^(t+1)次方上去,知道找到一个最小的t就是我们要找的k了.

#include <cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<string>
#define min(a,b) (a<b?a:b)
using namespace std;
typedef long long ll;
/*
还是因为数据的问题,中间的运算又超出了范围,导致溢出
我们以后看见数字的题,我们所有的变量同一long long
*/

const int maxn=1e5+10;
ll n,x;
ll val[maxn];
const int mod=1e9+7;

ll quick_pow(ll x,ll n)
{
    ll ans=1;
    x=x%mod;
    while(n){
        if(n&1)
            ans=ans*x%mod;
        x=x*x%mod;
        n>>=1;
    }
    return ans;
}

int main()
{
    scanf("%I64d %I64d",&n,&x);
    ll sum=0;
    for(int i=0;i<n;i++){
        scanf("%I64d",&val[i]);
        sum += val[i];
    }
    for(int i=0;i<n;i++)
        val[i]=sum-val[i];
    sort(val,val+n);
    ll cnt=1;
    val[n]=-1;
    ll min_num;
    for(int i=1;i<=n;i++){
        if(val[i]==val[i-1])
            cnt++;
        else{
          if(cnt%x){
              min_num=val[i-1];
              break;
          }
          else{
             val[i-1]++;
             cnt/=x;
             i--;//返回上一个点,看看前面
          }
        }
    }
    ll ans_num=min(min_num,sum);
    printf("%I64d\n",quick_pow(x,ans_num));
    return 0;
}


引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值