原题链接: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. 2019牛客多校第一场I Points Division(DP)题解

    题意: n个点,分成两组A,B,如果点i在A中,那么贡献值\(a_i\),反之为\(b_i\). 现要求任意\(i \in A,j \in B\)不存在 \(x_i >= x_j\) 且 \(y ...

  2. element-ui dialog loading

    element-ui dialog loading 指令方式 服务方式 v-loading 当使用指令方式时,全屏遮罩需要添加fullscreen修饰符(遮罩会插入至 body 上),此时若需要锁定屏 ...

  3. CSS :nth-of-type() bug

    CSS :nth-of-type() bug .tools-hover-box-list-item { pointer-events: auto; box-sizing: border-box; di ...

  4. Linux 创建/编辑/查看 文件/文件夹的命令汇总

    Linux 创建/编辑/查看 文件/文件夹的命令汇总 Linux 创建文件的命令Linux,编辑文件的命令Linux 查看文件的命令,touch,vim,vi,gedit,cat,ls -a, ls ...

  5. LeetCode 算法题解 js 版 (001 Two Sum)

    LeetCode 算法题解 js 版 (001 Two Sum) 两数之和 https://leetcode.com/problems/two-sum/submissions/ https://lee ...

  6. gradient text & gradient background

    gradient text & gradient background -webkit-text-fill-color & -webkit-gradient https://wesbo ...

  7. mdn & remove & removeChild

    mdn & remove & removeChild Element https://developer.mozilla.org/en-US/docs/Web/API/Element ...

  8. 闲着蛋疼看下a++的过程

    赋值过程 int a = 1; int b = a++; x86 反汇编: int a = 1; 00D06428 C7 45 F8 01 00 00 00 mov dword ptr [a],1 i ...

  9. NGK又双叒叕送钱了!百万SPC空投不要错过!

    不知不觉,2021年已然到来.回顾过去一年,2020年币圈发生的事情真的是太多太多,比特币的持续暴涨,DeFi一波又一波的空投福利,都让我们见识了区块链的魅力!同样,2021年区块链市场的牛市仍然持续 ...

  10. django学习-27.admin管理后台里:对列表展示页面的数据展示进行相关优化

    目录结构 1.前言 2.完整的操作步骤 2.1.第一步:查看ModelAdmin类和BaseModelAdmin类的源码 2.2.第二步:查看表animal对应的列表展示页面默认的数据展示 2.3.第 ...