B. Johnny and Grandmaster
原题链接: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的更多相关文章
- Codeforces 1103 C. Johnny Solving
Codeforces 1103 C. Johnny Solving 题目大意: 有一张 \(n\) 个点 \(m\) 条边的简单无向图,每个点的度数至少为 \(3\) ,你需要构造出两种情况之一 一条 ...
- 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 一次操作 ...
- SPOJ 274 Johnny and the Watermelon Plantation(TLE)
O(n^3)的时间复杂度,改了半天交了二三十遍,TLE到死,实在没办法了…… 跪求指点!!! #include <cstdio> #include <cstdlib> #inc ...
- Johnny Solving CodeForces - 1103C (构造,图论)
大意: 无向图, 无重边自环, 每个点度数>=3, 要求完成下面任意一个任务 找一条结点数不少于n/k的简单路径 找k个简单环, 每个环结点数小于n/k, 且不为3的倍数, 且每个环有一个特殊点 ...
- CodeForces 1103C. Johnny Solving
题目简述:给定简单(无自环.无重边)连通无向图$G = (V, E), 1 \leq n = |V| \leq 2.5 \times 10^5, 1 \leq m = |E| \leq 5 \time ...
- CF1103C Johnny Solving (Codeforces Round #534 (Div. 1)) 思维+构造
题目传送门 https://codeforces.com/contest/1103/problem/C 题解 这个题还算一个有难度的不错的题目吧. 题目给出了两种回答方式: 找出一条长度 \(\geq ...
- Codeforces Round #647 (Div. 2) D. Johnny and Contribution(BFS)
题目链接:https://codeforces.com/contest/1362/problem/D 题意 有一个 $n$ 点 $m$ 边的图,每个结点有一个从 $1 \sim n$ 的指定数字,每个 ...
- Codeforces Round #647 (Div. 2) C. Johnny and Another Rating Drop(数学)
题目链接:https://codeforces.com/contest/1362/problem/C 题意 计算从 $0$ 到 $n$ 相邻的数二进制下共有多少位不同,考虑二进制下的前导 $0$ .( ...
- Codeforces Round #647 (Div. 2) B. Johnny and His Hobbies(枚举)
题目链接:https://codeforces.com/contest/1362/problem/B 题意 有一个大小及元素值均不超过 $1024$ 的正整数集合,求最小正整数 $k$,使得集合中的每 ...
随机推荐
- XSS脚本汇总
(1)普通的XSS JavaScript注入<SCRIPT SRC=http://***/XSS/xss.js></SCRIPT> (2)IMG标签XSS使用JavaScrip ...
- React Native & Security
React Native & Security https://reactnative.dev/docs/security React Native blogs https://reactna ...
- Visual Studio Online & Web 版 VS Code
Visual Studio Online & Web 版 VS Code https://online.visualstudio.com https://devblogs.microsoft. ...
- React & react-native & vue & cli & environment information & report bugs
React & react-native & vue & cli & environment information & report bugs cli che ...
- CSS3 & gradient & color & background
CSS3 & gradient & color & background css background https://developer.mozilla.org/en-US/ ...
- Nestjs 上传文件到七牛云
$ npm install qiniu import * as url from 'url'; import * as qiniu from 'qiniu'; @Post('upload') @Use ...
- 观点纠正,yarn和npm对比,今天yarn仍然比npm快吗
yarn和npm的区别和对比,网上很多了,不多说了. 只纠正一个观点:yarn仍然比npm快吗?不. 2016年,yarn刚刚发布,速度确实比npm快,于是网络上出现了好多推荐yarn的文章. 于是很 ...
- Java中print、printf、println的区别
Java中print.printf.println的区别 区别 print:标准输出,但不换行,不可以空参: println:标准输出,但会自动换行,可以空参,可以看做:println()相当于pri ...
- 死磕Spring之IoC篇 - 深入了解Spring IoC(面试题)
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读 Spring 版本:5.1. ...
- 解决windwos系统80端口被暂用无法发布(NGINX、TOMCAT、IIS)
原因: 一个操作系统有0-65535个端口,但是一个端口只能被一个应用程序使用.所以80端口只有一个,当开发发布时想用应用NGINX,TOMCAT,IIS发布时,如果有程序占用了,就无法使用了. 解决 ...