CF 209 Div2 (C) 快速幂 取分子最小的公约数

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <queue>     //CF 209 Div2 (C)   快速幂  取分子分母最小的公约数
#define LL long long  
#define N 100010
using namespace std;
const LL mod=1000000007;   
LL x, a[N]; 
LL f(LL k)
{
	LL ans=1, s=x;
	while(k)
	{
		if(k&1)
		{
			ans*=s;
			ans%=mod;
		}
		s=s*s;
		s%=mod;
		k>>=1;
	}
	return ans;
}

int main()
{
	int n, t;
	LL s, h, k;
	while(scanf("%d%I64d", &n, &x)!=EOF)
	{
		for(t=0, s=0; t<n; ++t)
		{
			scanf("%I64d", a+t);
			s+=a[t];
		}
		for(t=0; t<n; ++t)
			a[t]=s-a[t];   //分子为a[t],分母为s
		sort(a, a+n);
		h=a[0];
		t=1;
		k=1;   //相同分子的个数
		while(t<n)
		{
			if(h==a[t])
			{
				k++;
				t++;
			}
			else 
			{
				if(k%x==0)  //换算
				{
					h++;   //一个个加上去,防止超过
					k=k/x; //k除后表示有多少个和h相同的数
				}
				else break;  //一旦不能整除则此时h已经是最小
			}
		}
		while(k%x==0)
		{
			h++;
			k=k/x;
		}
		h=min(h, s);   //分子分母作比较
		printf("%I64d\n", f(h));
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值