人生第一场 AtCoder,纪念一下

话说年后的 AtCoder 比赛怎么这么少啊(大雾

AtCoder Beginner Contest 154 题解

A - Remaining Balls

We have A balls with the string S written on each of them and B balls with the string T written on each of them.

From these balls, Takahashi chooses one with the string U written on it and throws it away.

Find the number of balls with the string S and balls with the string T that we have now.

Solution

#include <bits/stdc++.h>
using namespace std; int a,b;
string s,t,u; int main() {
cin>>s>>t>>a>>b>>u;
if(s==u) cout<<a-1<<" "<<b<<endl;
else cout<<a<<" "<<b-1<<endl;
}

B - I miss you...

Given is a string S. Replace every character in S with x and print the result.

Solution

算一下字符串长度即可,理论上按char读进来逐个输出应该更短

#include <bits/stdc++.h>
using namespace std; string s;
int main() {
cin>>s;
for(int i=0;i<s.length();i++) cout<<"x";
}

C - Distinct or Not

Given is a sequence of integers \(A_1, A_2, ..., A_N\). If its elements are pairwise distinct, print YES; otherwise, print NO.

Solution

排序应该是比较优雅的方法吧,虽然感觉 std::map 会更短

不对,这 \(1024MB\)的内存,是不是暴力开桶不用压位都能过

#include <bits/stdc++.h>
using namespace std; int a[200005],n; int main() {
ios::sync_with_stdio(false);
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
sort(a+1,a+n+1);
for(int i=1;i<n;i++) {
if(a[i]==a[i+1]) {
cout<<"NO"<<endl;
return 0;
}
}
cout<<"YES"<<endl;
}

D - Dice in Line

We have \(N\) dice arranged in a line from left to right. The \(i\)-th die from the left shows \(p_i\) numbers from \(1\) to \(p_i\) with equal probability when thrown.

We will choose \(K\) adjacent dice, throw each of them independently, and compute the sum of the numbers shown. Find the maximum possible value of the expected value of this sum.

Solution

根据期望的线性性质,很容易发现只要求个最大区间和就可以了。怎么求呢,前缀和啊。

因为没加fixed数字大时飘成科学计数法 WA 了一发,我 TM 真是个憨憨。

#include <bits/stdc++.h>
using namespace std; int n,k,p[200005]; int main() {
ios::sync_with_stdio(false);
cin>>n>>k;
for(int i=1;i<=n;i++) cin>>p[i], p[i]+=p[i-1];
int mx=0;
for(int i=0;i+k<=n;i++) mx=max(mx,p[i+k]-p[i]);
cout<<setiosflags(ios::fixed)<<setprecision(12)<<(k+mx)*0.5;
}

E - Almost Everywhere Zero

Find the number of integers between \(1\) and \(N\) (inclusive) that contains exactly \(K\) non-zero digits when written in base ten.

\(1 \leq N < 10^{100}\),

\(1 \leq K \leq 3\)

Solution

暴力数位 dp 即可,当然可能有更简单的方法,但我觉得推推公式什么的太麻烦了,还是直接数位 dp 吧

套路性地,设 \(f[i][j]\) 表示长度为 \(i\) 的数字串,有 \(j\) 个非零数位的方案数,转移方程

\[f[i][j] = f[i-1][j] + 9f[i-1][j-1]
\]

注意 \(i=0\) 或者 \(j=0\) 的时候需要特判一下

暴力转移预处理出 \(f[i][j]\) 后,我们来统计答案。先把 \(N\) 本身判掉,然后枚举 \(x\) 从哪一位开始比 \(N\) 小,那么这一位之前的就全部确定了(和 \(N\) 一样),这一位讨论一下是 \(0\) 和不是 \(0\) 的情况,每种情况下,这位之后的部分都只约束了非零数字的个数,求和即可得到答案。

#include <bits/stdc++.h>
using namespace std; #define int long long
const int N = 1005; char str[N];
int n,k,ans,f[N][5]; signed main() {
cin>>str+1;
n=strlen(str+1);
for(int i=1;i<=n;i++) str[i]-='0';
cin>>k;
f[0][0]=1;
for(int i=1;i<=n;i++) {
f[i][0]=f[i-1][0];
for(int j=1;j<=3;j++) {
f[i][j]=f[i-1][j]+9*f[i-1][j-1];
}
}
int cnt=0;
for(int i=1;i<=n;i++) {
//Calculate a[i] = 0
if(str[i]) {
if(k-cnt>=0) ans+=f[n-i][k-cnt];
}
//Calculate a[i] > 0
if(str[i]>1) {
if(k-cnt-1>=0) ans+=(str[i]-1)*f[n-i][k-cnt-1];
}
if(str[i]) ++cnt;
}
if(cnt==k) ++ans;
cout<<ans;
}

F - Many Many Paths

Snuke is standing on a two-dimensional plane. In one operation, he can move by \(1\) in the positive x-direction, or move by \(1\) in the positive y-direction.

Let us define a function \(f(r, c)\) as follows:

\(f(r,c) :=\) (The number of paths from the point \((0, 0)\) to the point \((r, c)\) that Snuke can trace by repeating the operation above)

Given are integers \(r_1, r_2, c_1,\) and \(c_2\). Find the sum of \(f(i, j)\) over all pair of integers \((i, j)\) such that \(r_1 ≤ i ≤ r_2\) and \(c_1 ≤ j ≤ c_2\), and compute this value modulo \((10^9+7)\).

\(1 ≤ r_1 ≤ r_2 ≤ 10^6\),

\(1 ≤ c_1 ≤ c_2 ≤ 10^6\)

Solution

首先单个答案是容易求的,根据高中数学可知 \(f(i,j) = C_{i+j}^i\)

设 \(g(i,j)\) 是它的二维前缀和,那么原答案一定可以用四个 \(g(i,j)\) 的和差表示

下面考虑如何求 \(g(i,j)\),打印一张数表看一看,很容易想到沿着 \(j\) 维度方向做差试试,观察容易得到

\[g(i,j)-g(i,j-1)=f(i,j+1)
\]

于是得到

\[g(i,j) = g(i,0) + \sum_{k=2}^{j-1} f(i,k)
\]

考虑到 \(g(i,0)\) 是显然的,而 \(f(i,j)\) 很容易做单维度递推,即

\[f(i,j) = f(i,j-1) \cdot (i+j) \cdot j^{-1}
\]

后者用逆元处理即可,每次逆元计算(使用快速幂方法)花费 \(O(\log n)\),于是我们可以在 \(O(n \log n)\) 时间内求出 \(\sum_j f(i,j)\),即求出了 \(g(i,j)\)

总体时间复杂度 \(O(n \log n)\)

#include <bits/stdc++.h>
using namespace std; #define int long long
#define ll long long
const int mod = 1e+9+7;
ll qpow(ll p,ll q) {
ll r = 1;
for(; q; p*=p, p%=mod, q>>=1) if(q&1) r*=p, r%=mod;
return r;
}
int inv(int p) {
return qpow(p,mod-2);
} const int N = 1e+6+5;
int f[N],g[N]; int solve(int i,int m) {
memset(f,0,sizeof f);
memset(g,0,sizeof g);
g[0]=i+1; f[1]=i+1;
for(int k=2;k<=m+1;k++) f[k]=f[k-1]*(i+k)%mod*inv(k)%mod;
for(int j=1;j<=m;j++) g[j]=(g[j-1]+f[j+1])%mod;
return g[m];
} signed main() {
int r1,c1,r2,c2;
cin>>r1>>c1>>r2>>c2;
--r1; --c1;
cout<<((solve(r2,c2)-solve(r1,c2)-solve(r2,c1)+solve(r1,c1)%mod+mod)%mod)<<endl;
}

AtCoder Beginner Contest 154 题解的更多相关文章

  1. AtCoder Beginner Contest 153 题解

    目录 AtCoder Beginner Contest 153 题解 A - Serval vs Monster 题意 做法 程序 B - Common Raccoon vs Monster 题意 做 ...

  2. AtCoder Beginner Contest 177 题解

    AtCoder Beginner Contest 177 题解 目录 AtCoder Beginner Contest 177 题解 A - Don't be late B - Substring C ...

  3. AtCoder Beginner Contest 184 题解

    AtCoder Beginner Contest 184 题解 目录 AtCoder Beginner Contest 184 题解 A - Determinant B - Quizzes C - S ...

  4. AtCoder Beginner Contest 173 题解

    AtCoder Beginner Contest 173 题解 目录 AtCoder Beginner Contest 173 题解 A - Payment B - Judge Status Summ ...

  5. AtCoder Beginner Contest 172 题解

    AtCoder Beginner Contest 172 题解 目录 AtCoder Beginner Contest 172 题解 A - Calc B - Minor Change C - Tsu ...

  6. AtCoder Beginner Contest 169 题解

    AtCoder Beginner Contest 169 题解 这场比赛比较简单,证明我没有咕咕咕的时候到了! A - Multiplication 1 没什么好说的,直接读入两个数输出乘积就好了. ...

  7. AtCoder Beginner Contest 148 题解

    目录 AtCoder Beginner Contest 148 题解 前言 A - Round One 题意 做法 程序 B - Strings with the Same Length 题意 做法 ...

  8. AtCoder Beginner Contest 151 题解报告

    总的来说,这次的题目比较水,然而菜菜的我并没有把所有题目都做完,话不多说,直接来干货: A:Next Alphabet 题目链接:https://atcoder.jp/contests/abc151/ ...

  9. AtCoder Beginner Contest 115 题解

    题目链接:https://abc115.contest.atcoder.jp/ A Christmas Eve Eve Eve 题目: Time limit : 2sec / Memory limit ...

随机推荐

  1. Java架构-高并发的解决实战总结方案

    Java架构-高并发的解决实战总结方案 1.应用和静态资源分离 刚开始的时候应用和静态资源是保存在一起的,当并发量达到一定程度的时候就需要将静态资源保存到专门的服务器中,静态资源主要包括图片.视频.j ...

  2. iMacros 入门教程-基础函数介绍(2)

    imacros 的 pos 参数是什么意思 position的缩写,如果有 2 个以上的元素共用完全相同的属性(比方说同一个小区的同一栋楼),这个 POS 的参数可以借由不同位置来帮助明确定位(也就是 ...

  3. 高精度模板 支持各种运算 c++

    绪言 自从有了高精度模板,妈妈再也不用怕我不会打高精度了! 代码 代码长度与日俱增啊~~~ #include<iostream> #include<cstring> #incl ...

  4. Wix 快速开发安装包程序 (二)安装行为

    上一小节,主要介绍了构建最小级别的安装包,这个安装包所做的事情很简单,主要是打包好一些文件,然后放到用户机器的某个位置下面. 这个小节,主要是总结安装过程的各种行为如何使用Wix编写. 一.写注册表 ...

  5. 【Spring】bean的作用域(@Scope) - singleton、prototype

    已知spring 3+已拥有多种不同的作用域: singleton(默认).prototype.request.session.global session.(参考: spring中scope作用域( ...

  6. Linux systemctl系统工具常用总结(详)

    systemctl是一个系统自带的服务管理工具,可以管理系统的服务的,启动.停止.重启.自启.监视.也可以对脚本程序后台运行管理. 文章以nginx.service举例 基础命令: systemctl ...

  7. VMware与Centos系统安装之重置root密码

    VMware与Centos系统安装之重置root密码   今日任务 1.Linux发行版的选择 2.vmware创建一个虚拟机(centos) 3.安装配置centos7 4.xshell配置连接虚拟 ...

  8. sc 与 net 命令

    查看命令的帮助: help sc 或者 help net net: net start mysql  打开服务 net stop mysql 关闭服务 net pause mysql 暂停服务 sc ...

  9. Bazinga HDU - 5510【技巧暴力+字符串】

    题目:https://vjudge.net/problem/HDU-5510 $2015ACM/ICPC$ 亚洲区沈阳站 题目大意: 输入$t$(表示样例个数) 如何每个样例一个 $n$,表示字符串的 ...

  10. 论文阅读笔记(二十)【AAAI2019】:Spatial and Temporal Mutual Promotion for Video-Based Person Re-Identification

    Introduction (1)Motivation: 作者考虑到空间上的噪声可以通过时间信息进行弥补,其原因为:不同帧的相同区域可能是相似信息,当一帧的某个区域存在噪声或者缺失,可以用其它帧的相同区 ...