Codeforces Round #341 (Div. 2)
在家都变的懒惰了,好久没写题解了,补补CF
模拟 A - Wet Shark and Odd and Even
#include <bits/stdc++.h> typedef long long ll;
const int N = 1e5 + 5;
const int INF = 0x3f3f3f3f; int main(void) {
std::vector<int> vec;
int n; scanf ("%d", &n);
ll sum = 0;
for (int x, i=1; i<=n; ++i) {
scanf ("%d", &x);
sum += x;
if (x & 1) vec.push_back (x);
}
std::sort (vec.begin (), vec.end ());
int sz = vec.size ();
if (sz > 0 && (sz & 1)) sum -= vec[0];
printf ("%I64d\n", sum); return 0;
}
开始想错了,当成斜率相等的。还好1000范围不大,统计矩阵每条对角线上的个数加点小优化就过了,代码丑。。。
#include <bits/stdc++.h> typedef long long ll;
const int N = 2e5 + 5;
bool vis[2][1005][1005];
int b[1005][1005];
std::pair<int, int> a[N]; ll cal(int x) {
return 1ll * x * (x - 1) / 2;
}
int get_num1(int x, int y) {
int xx = x, yy = y;
int ret = 0;
while (xx >= 1 && yy >= 1) {
if (b[xx][yy]) ret++, vis[1][xx][yy] = true;
xx--; yy--;
}
xx = x + 1, yy = y + 1;
while (xx <= 1000 && yy <= 1000) {
if (b[xx][yy]) ret++, vis[1][xx][yy] = true;
xx++; yy++;
}
return ret;
} int get_num0(int x, int y) {
int xx = x, yy = y;
int ret = 0;
while (xx <= 1000 && yy >= 1) {
if (b[xx][yy]) ret++, vis[0][xx][yy] = true;
xx++; yy--;
}
xx = x - 1, yy = y + 1;
while (xx >= 1 && yy <= 1000) {
if (b[xx][yy]) ret++, vis[0][xx][yy] = true;
xx--; yy++;
}
return ret;
} int main(void) {
int n; scanf ("%d", &n);
for (int i=0; i<n; ++i) {
scanf ("%d%d", &a[i].first, &a[i].second);
b[a[i].first][a[i].second] = 1;
}
ll ans = 0;
for (int i=0; i<n; ++i) {
int x = a[i].first, y = a[i].second;
if (!vis[0][x][y]) {
ans += cal (get_num0 (x, y));
vis[0][x][y] = true;
}
if (!vis[1][x][y]) {
ans += cal (get_num1 (x, y));
vis[1][x][y] = true;
}
}
printf ("%I64d\n", ans); return 0;
}
E = sum (1000 * 乘积是p的倍数的概率)
#include <bits/stdc++.h> typedef long long ll;
const int N = 1e5 + 5;
const int EPS = 1e-8;
int l[N], r[N];
double pos[N]; int main(void) {
int n, p; scanf ("%d%d", &n, &p);
for (int i=1; i<=n; ++i) {
scanf ("%d%d", &l[i], &r[i]);
pos[i] = 1.0 * (r[i] / p - ((l[i]-1) / p)) / (r[i] - l[i] + 1);
}
l[0] = l[n], r[0] = r[n], pos[0] = pos[n];
double ans = 0;
for (int i=0; i<=n; ++i) {
if (i < n) ans += pos[i] * 1.0 + (1.0 - pos[i]) * pos[i+1];
if (i > 0) ans += pos[i] * 1.0 + (1.0 - pos[i]) * pos[i-1];
}
ans *= 1000;
printf ("%.8f\n", ans); return 0;
}
数学(浮点) D - Rat Kwesh and Cheese
都取log,用long double,精度逆天!powl (): pow的long double版
C语言里对float类型数据的表示范围为-3.4*10^38~+3.4*10^38。double为-1.7*10^-308~1.7*10^308,long double为-1.2*10^-4932~1.2*10^4932.
类型 |
比特(位)数 |
有效数字 |
数值范围 |
float |
32 |
6~7 |
-3.4*10^38~+3.4*10^38 |
double |
64 |
15~16 |
-1.7*10^-308~1.7*10^308 |
long double |
128/ |
18~19 |
-1.2*10^-4932~1.2*10^4932 |
究竟如何计算该范围,分析如下:
对于单精度浮点数(float)来说,符号位一位,指数位8位,尾数23位。指数能够表示的指数范围为-128~127。尾数为23位。
#include <bits/stdc++.h> std::string ans[12] = {
"x^y^z", "x^z^y", "(x^y)^z", "(x^z)^y", "y^x^z", "y^z^x",
"(y^x)^z", "(y^z)^x", "z^x^y", "z^y^x", "(z^x)^y", "(z^y)^x"
}; typedef long double ldouble;
const double EPS = 1e-10;
ldouble best;
int id; bool better(ldouble val) {
if (fabs (val - best) < EPS) return false;
else if (best < val) {
best = val;
return true;
}
return false;
} void try2(ldouble x, ldouble y, ldouble z, int pos) {
//(x ^ y) ^ z
ldouble val = z * y * log (x);
if (better (val)) id = pos;
} void try1(ldouble x, ldouble y, ldouble z, int pos) {
//x ^ y ^ z
ldouble val = powl (y, z) * log (x);
if (better (val)) id = pos;
} int main(void) {
ldouble x, y, z; std::cin >> x >> y >> z;
best = -1e12; id = -1;
try1 (x, y, z, 0);
try1 (x, z, y, 1);
try2 (x, y, z, 2);
try2 (x, z, y, 3);
try1 (y, x, z, 4);
try1 (y, z, x, 5);
try2 (y, x, z, 6);
try2 (y, z, x, 7);
try1 (z, x, y, 8);
try1 (z, y, x, 9);
try2 (z, x, y, 10);
try2 (z, y, x, 11);
std::cout << ans[id] << '\n'; return 0;
}
数位DP(矩阵快速幂优化) E - Wet Shark and Blocks
题意:b组相同的n个数字,每组选择一个数字合并起来%x == k的方案数。
分析:dp思路,dp[i][(j*10+t)%x] += dp[i-1][j]。但是b太大了选择用矩阵快速幂优化,构造矩阵m[i][j]表示从i到j的方案数,答案就是m[0][k],那么base矩阵就是小于x的某个数字转移到根据给出n个数字转移到另一个数字y的方案数。
UPD:
寒假排位赛出了这题,又不会做。
构造x*x大小的矩阵,m[i][j]表示从i(%x)到j(%x)的方案数,状态转移用矩阵乘法:i->j1,j2,j3...->k => i->k
#include <bits/stdc++.h>
using namespace std; typedef long long ll;
typedef vector<ll> Vec;
typedef vector<Vec> Mat;
const int N = 5e4 + 5;
const int MOD = 1e9 + 7;
int cnt[10];
int n, b, k, x; void add_mod(ll &a, ll b) {
a += b;
if (a >= MOD) a -= MOD;
} Mat matrix_mul(const Mat &A, const Mat &B) {
Mat ret(A.size(), Vec(B[0].size()));
for (int i=0; i<A.size(); ++i)
for (int j=0; j<A[0].size(); ++j) if (A[i][j])
for (int k=0; k<B[0].size(); ++k) if (B[j][k])
add_mod(ret[i][k], A[i][j]*B[j][k]%MOD);
return ret;
} Mat matrix_pow(Mat X, int n) {
Mat ret(X.size(), Vec(X.size()));
for (int i=0; i<X.size(); ++i) ret[i][i] = 1;
for (; n; n>>=1) {
if (n & 1) ret = matrix_mul(ret, X);
X = matrix_mul(X, X);
}
return ret;
} int main() {
scanf("%d%d%d%d", &n, &b, &k, &x);
for (int i=1; i<=n; ++i) {
int d;
scanf("%d", &d);
cnt[d]++;
}
Mat ans(100, Vec(100));
for (int i=0; i<x; ++i) {
for (int j=1; j<=9; ++j) {
ans[i][(i*10+j)%x] += cnt[j];
}
}
ans = matrix_pow(ans, b);
printf("%I64d\n", ans[0][k]);
return 0;
}
Codeforces Round #341 (Div. 2)的更多相关文章
- Codeforces Round #341 (Div. 2) E. Wet Shark and Blocks dp+矩阵加速
题目链接: http://codeforces.com/problemset/problem/621/E E. Wet Shark and Blocks time limit per test2 se ...
- Codeforces Round #341 (Div. 2) D. Rat Kwesh and Cheese 数学
D. Rat Kwesh and Cheese 题目连接: http://www.codeforces.com/contest/621/problem/D Description Wet Shark ...
- Codeforces Round #341 Div.2 D. Rat Kwesh and Cheese
嗯本来想着直接算出来不就行了吗 然后我想到了200^200^200....... 好吧其实也不难取两次log就行了 然后我第一次写出来log就写残了........... log里面的拆分要仔细啊.. ...
- Codeforces Round #341 Div.2 C. Wet Shark and Flowers
题意: 不概括了..太长了.. 额第一次做这种问题 算是概率dp吗? 保存前缀项中第一个和最后一个的概率 然后每添加新的一项 就解除前缀和第一项和最后一项的关系 并添加新的一项和保存的两项的关系 这里 ...
- Codeforces Round #341 Div.2 B. Wet Shark and Bishops
题意:处在同一对角线上的主教(是这么翻译没错吧= =)会相互攻击 求互相攻击对数 由于有正负对角线 因此用两个数组分别保存每个主教写的 x-y 和 x+y 然后每个数组中扫描重复数字k ans加上kC ...
- Codeforces Round #341 Div.2 A. Wet Shark and Odd and Even
题意是得到最大的偶数和 解决办法很简单 排个序 取和 如果是奇数就减去最小的奇数 #include <cstdio> #include <cmath> #include < ...
- Codeforces Round #341 (Div. 2) ABCDE
http://www.cnblogs.com/wenruo/p/5176375.html A. Wet Shark and Odd and Even 题意:输入n个数,选择其中任意个数,使和最大且为奇 ...
- Codeforces Round #341 (Div. 2) E - Wet Shark and Blocks
题目大意:有m (m<=1e9) 个相同的块,每个块里边有n个数,每个数的范围是1-9,从每个块里边取出来一个数组成一个数,让你求组成的方案中 被x取模后,值为k的方案数.(1<=k< ...
- Codeforces Round #341 (Div. 2) C. Mike and Chocolate Thieves 二分
C. Mike and Chocolate Thieves time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
随机推荐
- php原型模式的研究
<?php class Sea{} class EarthSea extends Sea{} class MarsSea extends Sea{} class Plains{} class E ...
- [Android Pro] Android 进程级别 和 oom_adj对应关系
一 : 前台进程 (Active Process): oom_adj为0. 前台进程包括 : 1 : 活动 正在前台接收用户输入 2:活动.服务与广播接收器正在执行一个onReceive事件的处理函数 ...
- Oracle BFILE备忘
创建目录 create or replace directory exp_dir as '/tmp'; 赋权 grant read, write on directory exp_dir to PUB ...
- 兼容iOS 10:配置获取隐私数据权限声明
原文链接 iOS 10的一大变化是更强的隐私数据保护.在文档中是这么描述的: You must statically declare your app’s intended use of protec ...
- 点击按钮对两个div的隐藏与显示进行切换
HTML: <button type="button" id="showHidden">点击切换div的隐藏与显示</button> ...
- C#中的变量及命名规则
变量: 1.作用 :可以让我们在计算机中存储数据 2.语法:变量类型 变量名=赋值: 3.常用的数据类型: int 整数类型 取值范围:最大2147483647;最小-214748364 ...
- Codeforces Round #324 (Div. 2) C (二分)
题目链接:http://codeforces.com/contest/734/problem/C 题意: 玩一个游戏,一开始升一级需要t秒时间,现在有a, b两种魔法,两种魔法分别有m1, m2种效果 ...
- Spring 注释 @Autowired 和@Resource
一. @Autowired和@Resource都可以用来装配bean,都可以写在字段上,或者方法上. 二. @Autowired属于Spring的:@Resource为JSR-250标准的注释,属于J ...
- 23.备忘录模式(Memento Pattern)
using System; using System.Collections.Generic; namespace ConsoleApplication6 { /// <summary> ...
- Pyqt 时时CPU使用情况
借鉴代码来自:https://github.com/hgoldfish/quickpanel 实现代码: # -*- coding:utf-8 -*- from __future__ import p ...