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

来源:互联网 发布:编程赚钱的网站 编辑:程序博客网 时间:2024/05/24 06:15
#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 100010using 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],分母为ssort(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;}

0 0
原创粉丝点击