比赛链接:https://atcoder.jp/contests/abc172/tasks

A - Calc

题意

给出一个正整数 $a$,计算 $a + a^2 + a^3$ 。($1 \le a \le 10$)

代码

#include <bits/stdc++.h>
using namespace std;
int main() {
int a; cin >> a;
cout << a + a * a + a * a * a;
}

B - Minor Change

题意

给出两个等长的字符串 $s$ 和 $t$,每次可以替换 $s$ 中的一个字符,问使 $s$ 和 $t$ 相等至少要替换多少字符。

题解

不同的字符是一定要替换的。

代码

#include <bits/stdc++.h>
using namespace std;
int main() {
string s, t; cin >> s >> t;
int cnt = 0;
for (int i = 0; i < s.size(); i++)
if (s[i] != t[i]) ++cnt;
cout << cnt << "\n";
}

C - Tsundoku

题意

有两摞书,一摞有 $n$ 本,从上至下每本需阅读 $a_i$ 分钟,一摞有 $m$ 本,从上至下每本需阅读 $b_i$ 分钟,问最多能在 $k$ 分钟内读多少本书。

题解

计算两摞书阅读时长的前缀和,枚举从第一摞书中读多少本,余下的时间用二分或双指针查找能在第二摞书中读多少本。

代码一

二分

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
int n, m, k; cin >> n >> m >> k;
ll pre_a[n + 1] = {};
for (int i = 0; i < n; i++) {
int x; cin >> x;
pre_a[i + 1] = pre_a[i] + x;
}
ll pre_b[m + 1] = {};
for (int i = 0; i < m; i++) {
int x; cin >> x;
pre_b[i + 1] = pre_b[i] + x;
}
int ans = 0;
for (int i = 0; i < n + 1; i++) {
ll ex = k - pre_a[i];
if (ex >= 0) {
int j = upper_bound(pre_b, pre_b + m + 1, ex) - pre_b - 1;
ans = max(ans, i + j);
}
}
cout << ans << "\n";
}

代码二

双指针

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
int n, m, k; cin >> n >> m >> k;
ll pre_a[n + 1] = {};
for (int i = 0; i < n; i++) {
int x; cin >> x;
pre_a[i + 1] = pre_a[i] + x;
}
ll pre_b[m + 1] = {};
for (int i = 0; i < m; i++) {
int x; cin >> x;
pre_b[i + 1] = pre_b[i] + x;
}
int ans = 0;
for (int i = 0, j = m; i < n + 1; i++) {
ll ex = k - pre_a[i];
if (ex >= 0) {
while (j >= 0 and pre_b[j] > ex) --j;
ans = max(ans, i + j);
}
}
cout << ans << "\n";
}

D - Sum of Divisors

题意

设 $f_{(x)}$ 为 $x$ 正因子的个数。计算 $\sum_{i = 1}^n i \times f_{(i)}$ 。

题解

筛得每个数的 $f_{(x)}$,求和即可。

代码一

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
const int N = 1e7 + 10; ll f[N]; int main() {
int n; cin >> n;
for (int i = 1; i <= n; i++)
for (int j = i; j <= n; j += i)
++f[j];
ll ans = 0;
for (int i = 1; i <= n; i++)
ans += i * f[i];
cout << ans << "\n";
}

代码二

代码一简化而得

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
int n; cin >> n;
ll ans = 0;
for (int i = 1; i <= n; i++)
for (int j = i; j <= n; j += i)
ans += j;
cout << ans << "\n";
}

E - NEQ

题意

给出 $n, m$,计算有多少对大小为 $n$ 的数列 $a, b$ 满足:

  • $1 \le a_i, b_i \le m$
  • $a_i \neq a_j\ \ \ if\ \ \ i \neq j$
  • $b_i \neq b_j\ \ \ if\ \ \ i \neq j$
  • $a_i \neq b_i$

题解

\begin{equation} A_m^n ( \sum_{i = 0}^n (-1)^{i} C_n^i A_{m - i}^{n - i}) \nonumber \end{equation}

  • $A_m^n$,$m$ 个数排 $n$ 个位置,即合法的 $a$ 的个数;
  • $\sum$,对于每个合法的 $a$ 来说,合法的 $b$ 的个数;
    • $(-1)^i$,容斥原理;
    • $C_n^i A_{m - i}^{n - i}$,从 $b$ 的 $n$ 个位置中选 $i$ 个位置与 $a$ 中的数相等,余下 $n - i$ 个位置共有 $m - i$ 个数可选;
      • 当 $i = 0$ 时,$C_n^i A_{m - i}^{n - i} = A_m^n$,即合法的 $b$ 的个数;
      • 当 $i \ge 1$ 时,$C_n^i A_{m - i}^{n - i}$ 即代表对 $a$ 来说不合法的 $b$ 的个数;
      • 所以右式即用容斥原理从合法的 $b$ 中减去对 $a$ 来说不合法的 $b$ 的个数。

代码

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
const int N = 5e5 + 10;
const int mod = 1e9 + 7; ll fac[N]; ll binpow(ll a, ll b) {
ll res = 1;
while (b > 0) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
} void init() {
fac[0] = 1;
for (int i = 1; i < N; i++)
fac[i] = fac[i - 1] * i % mod;
} ll inv(ll a) {
return binpow(a, mod - 2);
} ll A(ll n, ll m) {
return fac[n] * inv(fac[n - m]) % mod;
} ll C(ll n, ll m) {
return fac[n] * inv(fac[m]) % mod * inv(fac[n - m]) % mod;
} int main() {
init();
int n, m; cin >> n >> m;
ll sum = 0;
for (int i = 0; i <= n; i++) {
sum += binpow(-1, i) * C(n, i) * A(m - i, n - i) % mod;
sum = (sum + mod) % mod;
}
cout << A(m, n) * sum % mod << "\n";
}

AtCoder Beginner Contest 172的更多相关文章

  1. AtCoder Beginner Contest 172 题解

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

  2. AtCoder Beginner Contest 100 2018/06/16

    A - Happy Birthday! Time limit : 2sec / Memory limit : 1000MB Score: 100 points Problem Statement E8 ...

  3. AtCoder Beginner Contest 052

    没看到Beginner,然后就做啊做,发现A,B太简单了...然后想想做完算了..没想到C卡了一下,然后还是做出来了.D的话瞎想了一下,然后感觉也没问题.假装all kill.2333 AtCoder ...

  4. AtCoder Beginner Contest 053 ABCD题

    A - ABC/ARC Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Smeke has ...

  5. AtCoder Beginner Contest 136

    AtCoder Beginner Contest 136 题目链接 A - +-x 直接取\(max\)即可. Code #include <bits/stdc++.h> using na ...

  6. AtCoder Beginner Contest 137 F

    AtCoder Beginner Contest 137 F 数论鬼题(虽然不算特别数论) 希望你在浏览这篇题解前已经知道了费马小定理 利用用费马小定理构造函数\(g(x)=(x-i)^{P-1}\) ...

  7. AtCoder Beginner Contest 076

    A - Rating Goal Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Takaha ...

  8. AtCoder Beginner Contest 079 D - Wall【Warshall Floyd algorithm】

    AtCoder Beginner Contest 079 D - Wall Warshall Floyd 最短路....先枚举 k #include<iostream> #include& ...

  9. AtCoder Beginner Contest 064 D - Insertion

    AtCoder Beginner Contest 064 D - Insertion Problem Statement You are given a string S of length N co ...

随机推荐

  1. 算法历练之路——入学考试(JAVA)

    入学考试 时间限制: 1Sec 内存限制: 128MB 提交: 42 解决: 18 题目描述辰辰是个天资聪颖的孩子,他的梦想是成为世界 上最伟大的医师.为此,他想拜附近最有威望的医师为师.医师为了判断 ...

  2. Python_列表(list)

    list()类中的提供的操作 1.索引取值 li = [11,22,33,44,55] v1 = li[3] print(li[2]) #索引取出33 print(v1) #索引取出44 print( ...

  3. vim 手动添加脚本头部信息

    vim /root/.vimrc 8,1 全部 set autoindent set tabstop=5 set shiftwidth=4 function AddTitle() call setli ...

  4. pidof

    pidof 服务名称,就可以查看到服务占用的进程号

  5. 【Oracle】win7安装报错

    在WIN7上安装oracle 10g时,提示如下信息: 正在检查操作系统要求... 要求的结果: 5.0,5.1,5.2,6.0 之一 实际结果: 6.1 检查完成.此次检查的总体结果为: 失败 &l ...

  6. ctfhub技能树—信息泄露—备份文件下载—.DS_Store

    打开靶机 查看页面信息 使用dirsearch进行扫描 访问该网页,下载文件 使用Linux系统打开文件 发现一个特殊文件,使用浏览器打开 拿到flag 二.使用Python-dsstore工具查看该 ...

  7. oracle绑定变量测试及性能对比

    1.创建测试数据 2.查看cursor_sharing的值 SQL> show parameter cursor_sharing; NAME TYPE VALUE --------------- ...

  8. Table controls and tabstrip controls

    本文转载自http://www.cnblogs.com/clsoho/archive/2010/01/21/1653268.html ONTROLS Syntax Forms Declaration ...

  9. SQLHelper ------ python实现

    SQLHelper ------ python实现 1.第一种: import pymysql import threading from DBUtils.PooledDB import Pooled ...

  10. 基于scrapy框架的分布式爬虫

    分布式 概念:可以使用多台电脑组件一个分布式机群,让其执行同一组程序,对同一组网络资源进行联合爬取. 原生的scrapy是无法实现分布式 调度器无法被共享 管道无法被共享 基于 scrapy+redi ...