CodeForces - 359C Prime Number(数论)

Prime Number

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

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·27729 = 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,另外有一个序列a1,a2,.....an,让x^a[i]相加求和得到一个分数,分母为x^a[1]*x^a[2]*.....x^a[n],最后求分子和分母的最大公约数

很有想法的一道题,看大神的题解费了老半天劲才懂 敲打,好像离散数学前几章节就讲了类似的,(不过好像我并没有听0.0)


思路:
首先我们想啊,x是素数,所以最后分子和分母一定可以写成m*(x^k)的形式(m不能被x整除),所以最大公约数就是x^k,k为他们两个中比较小的那一个k

分母的k很容易找到,就是a[1]+a[2]+....+a[n],主要是分子的k

我们令sum=a[1]+a[2]+....+a[n],那么分子和分母可以写成这样的形式:(x^(sum-a[1])+x^(sum-a[2])+....+x^(sum-a[n]))/(x^sum)
很明显分子的k就是x^(sum-a[i])中最小的那一个sum-a[i],但是由于分子中可能会出现两个相同的次数k
比如   2^2+2^2=2*2^2=2^3,
那么这种情况下的k就为3而不是2了

所以我们需要判断一下,假如将分子的次数k从小到大排序后,出现两个相同的k,那么就将这两项加到一块,让其系数加一
如果前后两项并不相同,那么我们需要先判断一下它(次数k小的那一项)的系数m能否被素数x整除,如果能整除说明次数k可以增大(让其+1),并让m/=x
如果不能整除,那么这一项的k就是我们需要的分子的k了(k已经从小到大排过序,所以第一次找到的肯定就是答案)

然后和分母的次数k比较一下取较小的,最后快速幂就ok了

代码:
#include<stdio.h>
#include<algorithm>
using namespace std;

#define min(a,b) (a<b?a:b)
#define LL long long int
#define maxn 100000+10
#define g 1000000007
LL a[maxn];

LL quick_mod(LL a,LL b)
{
    LL ans=1;
    a=a%g;
    while(b)
    {
        if(b&1)
            ans=ans*a%g;
        a=a*a%g;
        b>>=1;
    }
    return ans;
}

int main()
{
    LL x,n,i,sum=0;
    scanf("%lld%lld",&n,&x);
    for(i=0;i<n;i++)
    {
        scanf("%lld",&a[i]);
        sum+=a[i];//分母的次数k
    }
    for(i=0;i<n;i++)
        a[i]=sum-a[i];//分子的次数k
    sort(a,a+n);
    LL ans,cnt=1;//cnt为系数
    a[n]=-1;
    for(i=1;i<=n;i++)
    {
        if(a[i]!=a[i-1])
        {
            if(cnt%x)
            {
                ans=a[i-1];
                break;
            }
            else
            {
                cnt/=x;
                a[i-1]+=1;
                i--;
            }
        }
        else cnt++;
    }
    ans=min(ans,sum);
    printf("%lld\n",quick_mod(x,ans));
    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 ]
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值