Codeforces Round #460 (Div. 2) ABCDE题解
原文链接http://www.cnblogs.com/zhouzhendong/p/8397685.html
2018-02-01
$A$
题意概括
你要买$m$斤水果,现在有$n$个超市让你选择。
每个超市的水果价格是固定的。第$i$个超市的水果价格用两个整数$a_i和b_i$来表示。含义是$a_i$元可以买$b_i$斤。
问你买$m$斤水果最少花费多少钱。
题解
直接枚举超市,判断最少的花费就可以了。
代码
- #include <cstring>
- #include <cstdio>
- #include <cstdlib>
- #include <cmath>
- #include <algorithm>
- using namespace std;
- const int N=5005;
- int n;
- double m,ans=1e100;
- int main(){
- scanf("%d%lf",&n,&m);
- for (int i=1;i<=n;i++){
- int a,b;
- scanf("%d%d",&a,&b);
- ans=min(ans,m*a/b);
- }
- printf("%.10lf",ans);
- return 0;
- }
$B$
题意概括
找第$k$个各个数位之和为 10 的正整数。($k<=10000$)
题解
从1开始暴搜即可。每次把各个数位加起来判断一下就可以了。实测$k=10000$的时候大约答案为一千万左右。
代码
- #include <cstring>
- #include <cstdio>
- #include <cmath>
- #include <algorithm>
- #include <cstdlib>
- using namespace std;
- int k,ans=0;
- bool check(int v){
- int res=0;
- while (v){
- res+=v%10,v/=10;
- if (res>10)
- break;
- }
- return res==10;
- }
- int main(){
- scanf("%d",&k);
- while (k-=check(++ans));
- printf("%d",ans);
- return 0;
- }
$C$
题意概括
给一个$n \times m$的矩阵,里面'.'表示空,'*'表示有人,问你有多少个不同的$1 \times k$的全空矩阵。
1<=n,m,k<=2000
题解
我们分每行每列分别枚举,每个前缀'.'大于等于k的格子贡献为1.
然而本题有坑点,不少$hacker$借此大赚一把。
当$k=1$的时候,答案要除以2!!!
代码
- #include <cstring>
- #include <cstdio>
- #include <cmath>
- #include <algorithm>
- #include <cstdlib>
- using namespace std;
- const int N=2005;
- int n,m,k;
- char g[N][N];
- int main(){
- scanf("%d%d%d",&n,&m,&k);
- for (int i=1;i<=n;i++)
- scanf("%s",g[i]+1);
- int ans=0;
- for (int i=1;i<=n;i++){
- int cnt=0;
- for (int j=1;j<=m;j++){
- if (g[i][j]=='.')
- cnt++;
- else
- cnt=0;
- if (cnt>=k)
- ans++;
- }
- }
- for (int i=1;i<=m;i++){
- int cnt=0;
- for (int j=1;j<=n;j++){
- if (g[j][i]=='.')
- cnt++;
- else
- cnt=0;
- if (cnt>=k)
- ans++;
- }
- }
- if (k==1)
- ans/=2;
- printf("%d",ans);
- return 0;
- }
$D$
题意概括
有一个有$n$个节点,$m$条边的有向图,每一个节点i上面有一个字母$a_i$。
定义一条路径的价值为经过的所有节点中遇到的出现次数最多的字母的出现次数。
现在问你价值最大的路径的价值。(如果价值为$\infty$,输出$-1$)
$ 1 <= n,m <= 300000 , 'a'<= a_i <= 'z' $
题解
首先考虑 $-1$ 的情况。
显然,只要有环就是 $-1$,所以这个可以实现判掉。
于是剩下来的就是一个$DAG$了。
那么显然,由于字母只有26个,那么我们只需要拓扑排序 + $DP$就可以了。
具体怎么DP看代码。
代码
- #include <cstring>
- #include <cstdio>
- #include <cmath>
- #include <algorithm>
- #include <cstdlib>
- using namespace std;
- const int N=300005,M=300005;
- struct Gragh{
- int cnt,y[M],nxt[M],fst[N];
- void clear(){
- cnt=0;
- memset(fst,0,sizeof fst);
- }
- void add(int a,int b){
- y[++cnt]=b,nxt[cnt]=fst[a],fst[a]=cnt;
- }
- }g;
- int n,m,v[N],vis[N];
- int in[N],q[N],head,tail,dp[N][26];
- char s[N];
- bool dfs_round(int x,int mark){
- if (vis[x])
- return vis[x]==mark&&in[x];
- vis[x]=mark;
- in[x]=1;
- for (int i=g.fst[x];i;i=g.nxt[i])
- if (dfs_round(g.y[i],mark))
- return 1;
- in[x]=0;
- return 0;
- }
- bool round(){
- memset(vis,0,sizeof vis);
- memset(in,0,sizeof in);
- int mark=0;
- for (int i=1;i<=n;i++)
- if (!vis[i])
- if (dfs_round(i,++mark))
- return 1;
- return 0;
- }
- int main(){
- scanf("%d%d%s",&n,&m,s+1);
- for (int i=1;i<=n;i++)
- v[i]=s[i]-'a';
- g.clear();
- for (int i=1;i<=m;i++){
- int a,b;
- scanf("%d%d",&a,&b);
- g.add(a,b);
- }
- if (round()){
- puts("-1");
- return 0;
- }
- memset(dp,0,sizeof dp);
- memset(in,0,sizeof in);
- for (int i=1;i<=m;i++)
- in[g.y[i]]++;
- head=tail=0;
- for (int i=1;i<=n;i++)
- if (!in[i])
- q[++tail]=i;
- int ans=0;
- while (head<tail){
- int x=q[++head];
- dp[x][v[x]]++;
- for (int i=0;i<26;i++)
- ans=max(ans,dp[x][i]);
- for (int i=g.fst[x];i;i=g.nxt[i]){
- int y=g.y[i];
- for (int j=0;j<26;j++)
- dp[y][j]=max(dp[y][j],dp[x][j]);
- if (!--in[y])
- q[++tail]=y;
- }
- }
- printf("%d",ans);
- return 0;
- }
$E$
题意概括
问有多少个n满足以下条件:
$ 1<=n<=x $
$ n \cdot a^n \equiv b (mod \; p) $
输入$a b p x$,保证$p$是素数。
2 ≤ p ≤ 106 + 3, 1 ≤ a, b < p, 1 ≤ x ≤ 1012
题解
由于p为素数,根据费马小定理,$ a^k \equiv a^{k+(p-1)} (mod \; p) $
于是我们可以从$1$ ~ $p-1$枚举$n \bmod (p-1)$的值。
然后对于一个解$n\;mod\;(p-1) = i$,我们考虑到$a^n \equiv a^i (mod \; p )$, 所以,可以得到$n \equiv \frac{b}{a^i} (mod \; p)$
即$n \equiv b \times inv(a^i) (mod \; p)$
设$ x=i \; mod \; (p-1),\; y=b \times inv(a^i) \; mod \; p$,则有如下两个方程:
$n \equiv x\;(mod\;(p-1))$
$n \equiv y\;(mod\;p)$
这个讲道理有中国剩余定理(CRT)合并,但是实际上我们只需要瞎凑就可以了。
凑出来,$n\equiv -(p-1)\times y\; + \; p \times x (mod \; (p(p-1)))$
于是我们得到了关于$n$的一般形式,那么计算他的贡献就很轻松了(这个不讲了,直接看代码吧),总共$p-1$种$n$的形式,每个的贡献相加即答案。
代码
- #include <cstring>
- #include <cmath>
- #include <cstdlib>
- #include <cstdio>
- #include <algorithm>
- using namespace std;
- typedef long long LL;
- const int P=1e6+10;
- LL a,b,p,x,inv[P];
- LL Pow(LL x,LL y){
- if (!y)
- return 1;
- LL xx=Pow(x,y/2);
- xx=xx*xx%p;
- if (y&1LL)
- xx=xx*x%p;
- return xx;
- }
- LL Inv(LL x){
- return Pow(x,p-2);
- }
- int main(){
- scanf("%I64d%I64d%I64d%I64d",&a,&b,&p,&x);
- for (LL i=1;i<p;i++)
- inv[i]=Inv(i);
- LL ans=0;
- for (LL i=1;i<p;i++){
- LL y=b*inv[Pow(a,i)]%p;
- // n=y (mod p)
- // n=i (mod p-1)
- // n=up+y=v(p-1)+i
- // n=kp(p-1) - (p-1)*y + p*i
- LL mod=p*(p-1),n=(p*i%mod-(p-1)*y%mod+mod-1)%mod+1;
- if (n<=x)
- ans+=(x-n)/mod+1;
- }
- printf("%I64d",ans);
- return 0;
- }
Codeforces Round #460 (Div. 2) ABCDE题解的更多相关文章
- Codeforces Round #546 (Div. 2) ABCDE 题解
1136A: 题意:一本书有n个章节,每个章节的分别在li到ri页,小明读完书后将书折在第k页,问还有多少章节没有读 题解:控制k在li~ri的范围内后输出n-i即可 #include <set ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- Codeforces Round #261 (Div. 2)[ABCDE]
Codeforces Round #261 (Div. 2)[ABCDE] ACM 题目地址:Codeforces Round #261 (Div. 2) A - Pashmak and Garden ...
- # Codeforces Round #529(Div.3)个人题解
Codeforces Round #529(Div.3)个人题解 前言: 闲来无事补了前天的cf,想着最近刷题有点点怠惰,就直接一场cf一场cf的刷算了,以后的题解也都会以每场的形式写出来 A. Re ...
- Codeforces Round #557 (Div. 1) 简要题解
Codeforces Round #557 (Div. 1) 简要题解 codeforces A. Hide and Seek 枚举起始位置\(a\),如果\(a\)未在序列中出现,则对答案有\(2\ ...
- Codeforces Round #540 (Div. 3) 部分题解
Codeforces Round #540 (Div. 3) 题目链接:https://codeforces.com/contest/1118 题目太多啦,解释题意都花很多时间...还有事情要做,就选 ...
- Codeforces Round #538 (Div. 2) (A-E题解)
Codeforces Round #538 (Div. 2) 题目链接:https://codeforces.com/contest/1114 A. Got Any Grapes? 题意: 有三个人, ...
- Codeforces Round #531 (Div. 3) ABCDEF题解
Codeforces Round #531 (Div. 3) 题目总链接:https://codeforces.com/contest/1102 A. Integer Sequence Dividin ...
- Codeforces Round #527 (Div. 3) ABCDEF题解
Codeforces Round #527 (Div. 3) 题解 题目总链接:https://codeforces.com/contest/1092 A. Uniform String 题意: 输入 ...
随机推荐
- CSS rem长度单位
1. 概述 1.1 说明 rem是css3中新增的一个单位属性(font size of the root element),根据页面的根节点(html)的字体大小进行转换的单位,通过此单位属性可以进 ...
- python numpy中数组.min()
import numpy as np a = np.array([[1,5,3],[4,2,6]]) print(a.min()) #无参,所有中的最小值 print(a.min(0)) # axis ...
- PID控制器开发笔记之六:不完全微分PID控制器的实现
从PID控制的基本原理我们知道,微分信号的引入可改善系统的动态特性,但也存在一个问题,那就是容易引进高频干扰,在偏差扰动突变时尤其显出微分项的不足.为了解决这个问题人们引入低通滤波方式来解决这一问题. ...
- Ubuntu16.04安装MySQL
本篇教程在示例步骤中使用了以下版本的软件.操作时,请您以实际软件版本为准. 操作系统:Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-105-generic x86_64) ...
- |"|&|<|>等html字符转义
本文来源:d4shman < |"|&|<|>等html字符转义> 提示:请直接按CTRL+F搜索您要查找的转义字符 ...
- Django框架第一篇基础
一个小问题: 什么是根目录:就是没有路径,只有域名..url(r'^$') 补充一张关于wsgiref模块的图片 一.MTV模型 Django的MTV分别代表: Model(模型):和数据库相关的,负 ...
- hdu1540 区间合并+询问某点的最大连续块
询问操作需要搞一下 今天被区间合并降智了 /* D a: 摧毁第a个点 Q a:询问a所在的点的块大小 R :修复最后被破坏的点 对于所有的点需要进行一次更新 更新比较容易,tag用来表示区间是否是完 ...
- hdu5015构造转移矩阵
/* 构造转移矩阵: 先推公式: 首先是第0行:A[0][j+1]=A[0][j]*10+3 1-n行: A[i][j+1]=A[i][j]+A[i-1][j+1]=... =A[i][j]+A[i- ...
- uva11754 中国剩余定理+暴力搜索
是当y的组合数较小时,暴力枚举所有组合,然后用中国剩余定理求每种组合的解,对解进行排序即可 注意初始解可能是负数,所以如果凑不够S个,就对所有解加上M,2M.... 当y的组合数较大时,选择一个k/x ...
- vsftpd中的local_umask和anon_umask
umask是在linux中常见的一个东西,它其实是一个掩码.当然,也有umask这样一个命令,它是对用户建立的文件的默认属性的定义.该 定义为: 假设umask为022,则对于一个文件夹的话,它的默认 ...