原题链接:https://codeforc.es/problemset/problem/1361/B

题意:给你n个k求把pk分成两组数和的最小差值对1e9+7取余。

题解:运用贪心的思想取最大的数减去次大的数(先对数组按照降序排序),判断是否存在等于0的情况,如果存在那么最小差值为剩下数的和,如果不存在则答案为最大数减去其他数的和。(不存在小于0的情况)

坑点:1.不能用pow求幂(会超时),需要构造快速幂函数。

2.需要开另一个mod防止出现差值为1e9+7b倍数时产生的误差。

3.输入输出要用scanf 和printf 否则会超时。

Ac代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod1=1e9+7;
const ll mod2=0x3f3f3f3f;
const ll maxn=1e6+5;
ll a[maxn];
bool cmp(ll a,ll b){ //降序;
return a>b;
}
ll fastpow(ll a,ll n,ll m){ //快速幂;
ll base=a;
ll res=1;
while(n){
if(n&1){
res=(base*res)%m;
}
base=(base*base)%m;
n>>=1;
}
return res;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
ll n,p;
scanf("%lld%lld",&n,&p);
for(ll i=0;i<n;i++) scanf("%lld",&a[i]);
sort(a,a+n,cmp); //排序;
ll cnt1=0,cnt2=0;
for(ll i=0;i<n;i++){
if(cnt1==0&&cnt2==0){
cnt1=(cnt1+fastpow(p,a[i],mod1))%mod1 //防止cnt1%mod1==0对答案造成的影响;
cnt2=(cnt2+fastpow(p,a[i],mod2))%mod2;
}
else{
cnt1=(cnt1-fastpow(p,a[i],mod1)+mod1)%mod1;
cnt2=(cnt2-fastpow(p,a[i],mod2)+mod2)%mod2;
}
}
printf("%lld\n",cnt1);
}
return 0;
}

B. Johnny and Grandmaster的更多相关文章

  1. Codeforces 1103 C. Johnny Solving

    Codeforces 1103 C. Johnny Solving 题目大意: 有一张 \(n\) 个点 \(m\) 条边的简单无向图,每个点的度数至少为 \(3\) ,你需要构造出两种情况之一 一条 ...

  2. Codeforces Round #647 (Div. 2) - Thanks, Algo Muse! A、Johnny and Ancient Computer B、Johnny and His Hobbies C、Johnny and Another Rating Drop

    题目链接:A.Johnny and Ancient Computer 题意: 给你两个数a,b.问你可不可以通过左移位运算或者右移位运算使得它们两个相等.可以的话输出操作次数,不可以输出-1 一次操作 ...

  3. SPOJ 274 Johnny and the Watermelon Plantation(TLE)

    O(n^3)的时间复杂度,改了半天交了二三十遍,TLE到死,实在没办法了…… 跪求指点!!! #include <cstdio> #include <cstdlib> #inc ...

  4. Johnny Solving CodeForces - 1103C (构造,图论)

    大意: 无向图, 无重边自环, 每个点度数>=3, 要求完成下面任意一个任务 找一条结点数不少于n/k的简单路径 找k个简单环, 每个环结点数小于n/k, 且不为3的倍数, 且每个环有一个特殊点 ...

  5. CodeForces 1103C. Johnny Solving

    题目简述:给定简单(无自环.无重边)连通无向图$G = (V, E), 1 \leq n = |V| \leq 2.5 \times 10^5, 1 \leq m = |E| \leq 5 \time ...

  6. CF1103C Johnny Solving (Codeforces Round #534 (Div. 1)) 思维+构造

    题目传送门 https://codeforces.com/contest/1103/problem/C 题解 这个题还算一个有难度的不错的题目吧. 题目给出了两种回答方式: 找出一条长度 \(\geq ...

  7. Codeforces Round #647 (Div. 2) D. Johnny and Contribution(BFS)

    题目链接:https://codeforces.com/contest/1362/problem/D 题意 有一个 $n$ 点 $m$ 边的图,每个结点有一个从 $1 \sim n$ 的指定数字,每个 ...

  8. Codeforces Round #647 (Div. 2) C. Johnny and Another Rating Drop(数学)

    题目链接:https://codeforces.com/contest/1362/problem/C 题意 计算从 $0$ 到 $n$ 相邻的数二进制下共有多少位不同,考虑二进制下的前导 $0$ .( ...

  9. Codeforces Round #647 (Div. 2) B. Johnny and His Hobbies(枚举)

    题目链接:https://codeforces.com/contest/1362/problem/B 题意 有一个大小及元素值均不超过 $1024$ 的正整数集合,求最小正整数 $k$,使得集合中的每 ...

随机推荐

  1. mssql 2005安装

    SQL Server 2005详细安装过程及配置   说明:个人感觉SQL Server 2005是目前所有的SQL Server版本当中最好用的一个版本了,原因就是这个版本比起其它版本来说要安装简单 ...

  2. 第一个SpringMVC项目——HelloMVC

    第一步:启动IDEA新建一个无模板的Maven项目,注意这还不是一个web项目,想成为web项目,需要添加web框架,这就是第二步需要做的事情 第二步:添加web框架支持 单击之后会弹出这个界面 第三 ...

  3. cursor CSS属性定义鼠标指针悬浮在元素上时的外观。

    1 1 cursor CSS属性定义鼠标指针悬浮在元素上时的外观. https://developer.mozilla.org/zh-CN/docs/Web/CSS/cursor 概述 cursor  ...

  4. Android Studio & zh-Hans

    Android Studio & zh-Hans https://developer.android.com/studio?hl=zh-cn https://developer.android ...

  5. css animation & animation-fill-mode

    css animation & animation-fill-mode css animation effect https://developer.mozilla.org/en-US/doc ...

  6. background & background-image & border-image

    background & background-image & border-image https://developer.mozilla.org/en-US/docs/Web/CS ...

  7. Node.js & module.exports & exports

    Node.js & module.exports & exports https://www.cnblogs.com/xgqfrms/p/9493550.html exports &a ...

  8. SPC空投火爆来袭!区块链技术落地加速!

    经历市场狂热后,区块链逐渐恢复合理性,在政策红利.技术等多力推进下,各行各业开始涌入区块链行业.在这波浪潮中,SPC侧链代币项目显得格外亮眼,其空投已经发放至第二轮,仅SPC空投月收益就达23%左右, ...

  9. Python 与 excel的简单应用

    1.pip openpyxl库: pip install openpyxl -i http://pypi.douban.com/simple --trust-host pypi.douban.com ...

  10. idea加载maven项目遇见的坑---2

    idea每次加载完一个maven项目,都需要重新配置 file>>>Settings 然后继续找 还有就是配置项目的时候 配置项目jdk 最后需要注意,有时候你会发现都设置完了,但是 ...