原文链接http://www.cnblogs.com/zhouzhendong/p/8397685.html

2018-02-01

$A$

题意概括

  你要买$m$斤水果,现在有$n$个超市让你选择。

  每个超市的水果价格是固定的。第$i$个超市的水果价格用两个整数$a_i和b_i$来表示。含义是$a_i$元可以买$b_i$斤。

  问你买$m$斤水果最少花费多少钱。

题解

  直接枚举超市,判断最少的花费就可以了。

代码

  1. #include <cstring>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <algorithm>
  6. using namespace std;
  7. const int N=5005;
  8. int n;
  9. double m,ans=1e100;
  10. int main(){
  11. scanf("%d%lf",&n,&m);
  12. for (int i=1;i<=n;i++){
  13. int a,b;
  14. scanf("%d%d",&a,&b);
  15. ans=min(ans,m*a/b);
  16. }
  17. printf("%.10lf",ans);
  18. return 0;
  19. }

  

$B$

题意概括

  找第$k$个各个数位之和为 10 的正整数。($k<=10000$)

题解

  从1开始暴搜即可。每次把各个数位加起来判断一下就可以了。实测$k=10000$的时候大约答案为一千万左右。

代码

  1. #include <cstring>
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <algorithm>
  5. #include <cstdlib>
  6. using namespace std;
  7. int k,ans=0;
  8. bool check(int v){
  9. int res=0;
  10. while (v){
  11. res+=v%10,v/=10;
  12. if (res>10)
  13. break;
  14. }
  15. return res==10;
  16. }
  17. int main(){
  18. scanf("%d",&k);
  19. while (k-=check(++ans));
  20. printf("%d",ans);
  21. return 0;
  22. }

  

$C$

题意概括

  给一个$n \times m$的矩阵,里面'.'表示空,'*'表示有人,问你有多少个不同的$1 \times k$的全空矩阵。

  1<=n,m,k<=2000

题解

  我们分每行每列分别枚举,每个前缀'.'大于等于k的格子贡献为1.

  然而本题有坑点,不少$hacker$借此大赚一把。

  当$k=1$的时候,答案要除以2!!!

代码

  1. #include <cstring>
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <algorithm>
  5. #include <cstdlib>
  6. using namespace std;
  7. const int N=2005;
  8. int n,m,k;
  9. char g[N][N];
  10. int main(){
  11. scanf("%d%d%d",&n,&m,&k);
  12. for (int i=1;i<=n;i++)
  13. scanf("%s",g[i]+1);
  14. int ans=0;
  15. for (int i=1;i<=n;i++){
  16. int cnt=0;
  17. for (int j=1;j<=m;j++){
  18. if (g[i][j]=='.')
  19. cnt++;
  20. else
  21. cnt=0;
  22. if (cnt>=k)
  23. ans++;
  24. }
  25. }
  26. for (int i=1;i<=m;i++){
  27. int cnt=0;
  28. for (int j=1;j<=n;j++){
  29. if (g[j][i]=='.')
  30. cnt++;
  31. else
  32. cnt=0;
  33. if (cnt>=k)
  34. ans++;
  35. }
  36. }
  37. if (k==1)
  38. ans/=2;
  39. printf("%d",ans);
  40. return 0;
  41. }

$D$

题意概括

  有一个有$n$个节点,$m$条边的有向图,每一个节点i上面有一个字母$a_i$。

  定义一条路径的价值为经过的所有节点中遇到的出现次数最多的字母的出现次数。

  现在问你价值最大的路径的价值。(如果价值为$\infty$,输出$-1$)

  $ 1 <= n,m <= 300000 , 'a'<= a_i <= 'z' $

题解

  首先考虑  $-1$  的情况。

  显然,只要有环就是 $-1$,所以这个可以实现判掉。

  于是剩下来的就是一个$DAG$了。

  那么显然,由于字母只有26个,那么我们只需要拓扑排序 + $DP$就可以了。

  具体怎么DP看代码。

代码

  1. #include <cstring>
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <algorithm>
  5. #include <cstdlib>
  6. using namespace std;
  7. const int N=300005,M=300005;
  8. struct Gragh{
  9. int cnt,y[M],nxt[M],fst[N];
  10. void clear(){
  11. cnt=0;
  12. memset(fst,0,sizeof fst);
  13. }
  14. void add(int a,int b){
  15. y[++cnt]=b,nxt[cnt]=fst[a],fst[a]=cnt;
  16. }
  17. }g;
  18. int n,m,v[N],vis[N];
  19. int in[N],q[N],head,tail,dp[N][26];
  20. char s[N];
  21. bool dfs_round(int x,int mark){
  22. if (vis[x])
  23. return vis[x]==mark&&in[x];
  24. vis[x]=mark;
  25. in[x]=1;
  26. for (int i=g.fst[x];i;i=g.nxt[i])
  27. if (dfs_round(g.y[i],mark))
  28. return 1;
  29. in[x]=0;
  30. return 0;
  31. }
  32. bool round(){
  33. memset(vis,0,sizeof vis);
  34. memset(in,0,sizeof in);
  35. int mark=0;
  36. for (int i=1;i<=n;i++)
  37. if (!vis[i])
  38. if (dfs_round(i,++mark))
  39. return 1;
  40. return 0;
  41. }
  42. int main(){
  43. scanf("%d%d%s",&n,&m,s+1);
  44. for (int i=1;i<=n;i++)
  45. v[i]=s[i]-'a';
  46. g.clear();
  47. for (int i=1;i<=m;i++){
  48. int a,b;
  49. scanf("%d%d",&a,&b);
  50. g.add(a,b);
  51. }
  52. if (round()){
  53. puts("-1");
  54. return 0;
  55. }
  56. memset(dp,0,sizeof dp);
  57. memset(in,0,sizeof in);
  58. for (int i=1;i<=m;i++)
  59. in[g.y[i]]++;
  60. head=tail=0;
  61. for (int i=1;i<=n;i++)
  62. if (!in[i])
  63. q[++tail]=i;
  64. int ans=0;
  65. while (head<tail){
  66. int x=q[++head];
  67. dp[x][v[x]]++;
  68. for (int i=0;i<26;i++)
  69. ans=max(ans,dp[x][i]);
  70. for (int i=g.fst[x];i;i=g.nxt[i]){
  71. int y=g.y[i];
  72. for (int j=0;j<26;j++)
  73. dp[y][j]=max(dp[y][j],dp[x][j]);
  74. if (!--in[y])
  75. q[++tail]=y;
  76. }
  77. }
  78. printf("%d",ans);
  79. return 0;
  80. }

$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$的形式,每个的贡献相加即答案。

代码

  1. #include <cstring>
  2. #include <cmath>
  3. #include <cstdlib>
  4. #include <cstdio>
  5. #include <algorithm>
  6. using namespace std;
  7. typedef long long LL;
  8. const int P=1e6+10;
  9. LL a,b,p,x,inv[P];
  10. LL Pow(LL x,LL y){
  11. if (!y)
  12. return 1;
  13. LL xx=Pow(x,y/2);
  14. xx=xx*xx%p;
  15. if (y&1LL)
  16. xx=xx*x%p;
  17. return xx;
  18. }
  19. LL Inv(LL x){
  20. return Pow(x,p-2);
  21. }
  22. int main(){
  23. scanf("%I64d%I64d%I64d%I64d",&a,&b,&p,&x);
  24. for (LL i=1;i<p;i++)
  25. inv[i]=Inv(i);
  26. LL ans=0;
  27. for (LL i=1;i<p;i++){
  28. LL y=b*inv[Pow(a,i)]%p;
  29. // n=y (mod p)
  30. // n=i (mod p-1)
  31. // n=up+y=v(p-1)+i
  32. // n=kp(p-1) - (p-1)*y + p*i
  33. LL mod=p*(p-1),n=(p*i%mod-(p-1)*y%mod+mod-1)%mod+1;
  34. if (n<=x)
  35. ans+=(x-n)/mod+1;
  36. }
  37. printf("%I64d",ans);
  38. return 0;
  39. }

  

Codeforces Round #460 (Div. 2) ABCDE题解的更多相关文章

  1. Codeforces Round #546 (Div. 2) ABCDE 题解

    1136A: 题意:一本书有n个章节,每个章节的分别在li到ri页,小明读完书后将书折在第k页,问还有多少章节没有读 题解:控制k在li~ri的范围内后输出n-i即可 #include <set ...

  2. Codeforces Round #353 (Div. 2) ABCDE 题解 python

    Problems     # Name     A Infinite Sequence standard input/output 1 s, 256 MB    x3509 B Restoring P ...

  3. Codeforces Round #261 (Div. 2)[ABCDE]

    Codeforces Round #261 (Div. 2)[ABCDE] ACM 题目地址:Codeforces Round #261 (Div. 2) A - Pashmak and Garden ...

  4. # Codeforces Round #529(Div.3)个人题解

    Codeforces Round #529(Div.3)个人题解 前言: 闲来无事补了前天的cf,想着最近刷题有点点怠惰,就直接一场cf一场cf的刷算了,以后的题解也都会以每场的形式写出来 A. Re ...

  5. Codeforces Round #557 (Div. 1) 简要题解

    Codeforces Round #557 (Div. 1) 简要题解 codeforces A. Hide and Seek 枚举起始位置\(a\),如果\(a\)未在序列中出现,则对答案有\(2\ ...

  6. Codeforces Round #540 (Div. 3) 部分题解

    Codeforces Round #540 (Div. 3) 题目链接:https://codeforces.com/contest/1118 题目太多啦,解释题意都花很多时间...还有事情要做,就选 ...

  7. Codeforces Round #538 (Div. 2) (A-E题解)

    Codeforces Round #538 (Div. 2) 题目链接:https://codeforces.com/contest/1114 A. Got Any Grapes? 题意: 有三个人, ...

  8. Codeforces Round #531 (Div. 3) ABCDEF题解

    Codeforces Round #531 (Div. 3) 题目总链接:https://codeforces.com/contest/1102 A. Integer Sequence Dividin ...

  9. Codeforces Round #527 (Div. 3) ABCDEF题解

    Codeforces Round #527 (Div. 3) 题解 题目总链接:https://codeforces.com/contest/1092 A. Uniform String 题意: 输入 ...

随机推荐

  1. CSS rem长度单位

    1. 概述 1.1 说明 rem是css3中新增的一个单位属性(font size of the root element),根据页面的根节点(html)的字体大小进行转换的单位,通过此单位属性可以进 ...

  2. python numpy中数组.min()

    import numpy as np a = np.array([[1,5,3],[4,2,6]]) print(a.min()) #无参,所有中的最小值 print(a.min(0)) # axis ...

  3. PID控制器开发笔记之六:不完全微分PID控制器的实现

    从PID控制的基本原理我们知道,微分信号的引入可改善系统的动态特性,但也存在一个问题,那就是容易引进高频干扰,在偏差扰动突变时尤其显出微分项的不足.为了解决这个问题人们引入低通滤波方式来解决这一问题. ...

  4. Ubuntu16.04安装MySQL

      本篇教程在示例步骤中使用了以下版本的软件.操作时,请您以实际软件版本为准. 操作系统:Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-105-generic x86_64) ...

  5. |"|&|<|>等html字符转义

    本文来源:d4shman  <&nbsp|&quot|&amp|&lt|&gt等html字符转义> 提示:请直接按CTRL+F搜索您要查找的转义字符 ...

  6. Django框架第一篇基础

    一个小问题: 什么是根目录:就是没有路径,只有域名..url(r'^$') 补充一张关于wsgiref模块的图片 一.MTV模型 Django的MTV分别代表: Model(模型):和数据库相关的,负 ...

  7. hdu1540 区间合并+询问某点的最大连续块

    询问操作需要搞一下 今天被区间合并降智了 /* D a: 摧毁第a个点 Q a:询问a所在的点的块大小 R :修复最后被破坏的点 对于所有的点需要进行一次更新 更新比较容易,tag用来表示区间是否是完 ...

  8. 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- ...

  9. uva11754 中国剩余定理+暴力搜索

    是当y的组合数较小时,暴力枚举所有组合,然后用中国剩余定理求每种组合的解,对解进行排序即可 注意初始解可能是负数,所以如果凑不够S个,就对所有解加上M,2M.... 当y的组合数较大时,选择一个k/x ...

  10. vsftpd中的local_umask和anon_umask

    umask是在linux中常见的一个东西,它其实是一个掩码.当然,也有umask这样一个命令,它是对用户建立的文件的默认属性的定义.该 定义为: 假设umask为022,则对于一个文件夹的话,它的默认 ...