2021牛客寒假算法基础集训营6
2022/3/29 1:23:05
本文主要是介绍2021牛客寒假算法基础集训营6,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
题目链接
2021牛客寒假算法基础集训营6
F.组合数问题
题目描述
小 \(M\) 很喜欢组合数。
小 \(Z\)给了她一个数 \(\mathrm{n}\) ( \(\mathrm{n}\) 为偶数) ,让她计算 \(\left(\begin{array}{l}n \\ 0\end{array}\right)+\left(\begin{array}{l}n \\ 2\end{array}\right)+\left(\begin{array}{l}n \\ 4\end{array}\right) . .+\left(\begin{array}{l}n \\ n\end{array}\right)\) ,小 \(\mathrm{M}\) 一下就秒掉了,觉得题好简单。
因此,小\(Z\)给了她一个难题: 给定一个数 \(\mathrm{n}\) ( \(\mathrm{n}\) 是\(4\)的倍数),计算 \(\left(\begin{array}{l}n \\ 0\end{array}\right)+\left(\begin{array}{l}n \\ 4\end{array}\right)+\left(\begin{array}{l}n \\ 8\end{array}\right)+\ldots+\left(\begin{array}{l}n \\ n\end{array}\right)\) ,答案对 \(998244353\) 取模。
小 \(M\) 不会做,请你来帮帮她吧!
输入描述:
输入一个数 \(\mathrm{n}\) 。
输出描述:
输出答案对 \(998244353\) 取模的值。
解题思路
\[\begin{aligned} &S 1=(1+1)^{n}+(1-1)^{n}=2\left(\left(\begin{array}{l} n \\ 0 \end{array}\right)+\left(\begin{array}{l} n \\ 2 \end{array}\right)+\ldots+\left(\begin{array}{l} n \\ n \end{array}\right)\right) \\ &S 2=(1+i)^{n}+(1-i)^{n}=2\left(\left(\begin{array}{l} n \\ 0 \end{array}\right)-\left(\begin{array}{l} n \\ 2 \end{array}\right)+\ldots+\left(\begin{array}{l} n \\ n \end{array}\right)\right) \\ &S 1+S 2=4\left(\left(\begin{array}{l} n \\ 0 \end{array}\right)+\left(\begin{array}{c} n \\ 4 \end{array}\right)+\ldots+\left(\begin{array}{l} n \\ n \end{array}\right)\right) \\ &\text { 所以原式 }=\frac{2^{n}+(1+i)^{n}+(1-i)^{n}}{4} \text { ,用复数快速幂即可 } \end{aligned} \]数论,复数快速幂
- 时间复杂度:\(O(logn)\)
代码
// Problem: 组合数问题 // Contest: NowCoder // URL: https://ac.nowcoder.com/acm/contest/31272/F // Memory Limit: 524288 MB // Time Limit: 2000 ms // // Powered by CP Editor (https://cpeditor.org) // %%%Skyqwq #include <bits/stdc++.h> // #define int long long #define help {cin.tie(NULL); cout.tie(NULL);} #define pb push_back #define fi first #define se second #define mkp make_pair using namespace std; typedef long long LL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; } template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; } template <typename T> void inline read(T &x) { int f = 1; x = 0; char s = getchar(); while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); } while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar(); x *= f; } const int mod=998244353; LL n; struct Complex { LL x,y; Complex(LL _x,LL _y) { x=_x,y=_y; } }; void mul(Complex &a,Complex b,int p) { LL x=((a.x*b.x-a.y*b.y)%p+p)%p; LL y=((a.x*b.y+a.y*b.x)%p+p)%p; a={x,y}; } void add(Complex &a,Complex b,int p) { a.x=(a.x+b.x)%p; a.y=(a.y+b.y)%p; } Complex ksm(Complex a,LL b,int p) { Complex res(1,0); while(b) { if(b&1)mul(res,a,p); mul(a,a,p); b>>=1; } return res; } int ksm(int a,int b,int p) { int res=1; while(b) { if(b&1) res=1ll*res*a%p; a=1ll*a*a%p; b>>=1; } return res; } int main() { cin>>n; auto res=ksm({2,0},n,mod); add(res,ksm({1,1},n,mod),mod),add(res,ksm({1,-1},n,mod),mod); mul(res,ksm({4,0},mod-2,mod),mod); cout<<res.x; return 0; }
G.机器人
题目描述
题目描述
有 \(n\) 个机器人,每个机器人会读入一个 \(x\) ,并返回 \(ax+b\) 。
现在银临姐姐手里有一个数 \(x\) ,她想将机器人按某种顺序排列,使得最终返回得到的 \(x\) 尽可能大。
但是计算量太大啦,请你编个程序帮帮她吧。
输入描述:
第一行读入 \(n,x\) ,接下来 \(n\) 行依次输入 \(a_i, b_i\) 。
输出描述:
输出最大值。
示例1
输入
2 2 11 4 5 14
输出
268
备注:
对于所有的数据,\(1\le n,x,a_i,b_i\le 20\) 。
解题思路
贪心
只考虑两个机器人的话,有这样的顺序:\((a_i,b_i),(a_j,b_j)\),则有:\(a_j\times(a_ix+b_i)+b_j \geq a_i\times(a_jx+b_j)+b_i\),即 \(a_ib_j+b_i \leq a_jb_i+b_j\),对于多个机器人,如果存在相邻两项不满足这样的关系,则交换后会更优,另外注意会爆long long
- 时间复杂度:\(O(nlogn)\)
代码
// Problem: 机器人 // Contest: NowCoder // URL: https://ac.nowcoder.com/acm/contest/31272/G // Memory Limit: 1048576 MB // Time Limit: 6000 ms // // Powered by CP Editor (https://cpeditor.org) // %%%Skyqwq #include <bits/stdc++.h> //#define int long long #define help {cin.tie(NULL); cout.tie(NULL);} #define pb push_back #define fi first #define se second #define mkp make_pair using namespace std; typedef long long LL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; } template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; } template <typename T> void inline read(T &x) { int f = 1; x = 0; char s = getchar(); while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); } while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar(); x *= f; } template <typename T> void print(T x) { if (x < 0) putchar('-'), x = -x; if (x < 10) putchar(x + 48); else print(x / 10), putchar(x % 10 + 48); } int n; __int128 x; PII a[25]; int main() { cin>>n; read(x); for(int i=1;i<=n;i++)cin>>a[i].fi>>a[i].se; sort(a+1,a+1+n,[](PII &x,PII &y){return y.se*x.fi+x.se<x.se*y.fi+y.se;}); for(int i=1;i<=n;i++) x=x*a[i].fi+a[i].se; print(x); return 0; }
这篇关于2021牛客寒假算法基础集训营6的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-31全网首发第二弹!软考2024年5月《软件设计师》真题+解析+答案!(11-20题)
- 2024-05-31全网首发!软考2024年5月《软件设计师》真题+解析+答案!(21-30题)
- 2024-05-30【Java】百万数据excel导出功能如何实现
- 2024-05-30我们小公司,哪像华为一样,用得上IPD(集成产品开发)?
- 2024-05-30java excel上传--poi
- 2024-05-30安装笔记本应用商店的pycharm,再安排pandas等模块,说是没有打包工具?
- 2024-05-29java11新特性
- 2024-05-29哪些无用敏捷指标正在破坏敏捷转型?
- 2024-05-29鸿蒙原生应用再新丁!新华社 入局鸿蒙
- 2024-05-29设计模式 之 迭代器模式(Iterator)