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$,使得集合中的每 ...
随机推荐
- MS16-032 windows本地提权
试用系统:Tested on x32 Win7, x64 Win8, x64 2k12R2 提权powershell脚本: https://github.com/FuzzySecurity/Power ...
- mitmproxy 代理工具介绍:rewrite和map local实现
在接口测试中,会用到抓包工具或者代理工具,常用代理工具包括charles. burpsuite. fiddler.mitmproxy等,ssh -D参数 可实现socks5代理.网络嗅探工具可以使用t ...
- canvas画布基本知识点总结
HTML5的canvas元素使用JavaScript画图: <canvas width="600" height="400"> </canva ...
- Internationalization API & ECMA-402
Internationalization API & ECMA-402 i18n https://caniuse.com/?search=Internationalization API In ...
- VS Code & terminal & Canvas & DOM
VS Code & terminal & Canvas & DOM https://code.visualstudio.com/docs/editor/integrated-t ...
- push notifications
push notifications https://developers.google.com/web/fundamentals/push-notifications/ Push API https ...
- js & class & init
js & class & init how to call class init method in js when create an instance 在初始化类实例的时候调用,类 ...
- SMS OTP 表单最佳做法 (短信验证)
<form action="/verify-otp" method="POST"> <input type="text" ...
- nasm astrlen函数 x86
xxx.asm %define p1 ebp+8 %define p2 ebp+12 %define p3 ebp+16 section .text global dllmain export ast ...
- NGK福利再升级,1万枚VAST限时免费送
NGK在推出持有算力获得SPC空投活动后,福利再升级,于美国加州时间2021年2月8日下午4点推出新人福利活动,注册NGK成为新会员,即可获得0.2枚VAST奖励. VAST免费福利送活动仅送出1万枚 ...