Codeforces Round #219(Div. 2)373 B. Making Sequences is Fun(二分+找规律)
题目意思大概是给你w,m,k三个数,让你从m开始找 m m+1 m+2 m+3...........m+m', 使得他们的权值之和不超过w,计算权值的方法如下S(n)·k
。 S(n)表示n有多少位数,让你计算出最多有多少个这样的数。
乍一眼看去,10^16如此大的数肯定不能暴力,然后稍微思考一下就可以找到规律了,规律如下。
1-10 有9个一位数
1-100 有9个一位数 有90个二位数
1-1000 有9个一位数 有90个二位数 有900个三位数
1-10000 有9个一位数 有90个二位数 有900个三位数 有9000个四位数
。。。。。。。。。。。。
那么1-n有多少个几位数很快就可以计算出来了,然后用二分法计算结果。代码如下
#include <stdio.h>
#include <iostream>
#define ll long long
#define inf 100000000000000000
//二分上界必须大于10^16,不然会出错 using namespace std; ll w, m, k; ll getans(ll x)
{
ll cmp = 10;
ll add = 9;
ll ans = 0;
ll sum = 0;
ll cnt = 1;
while (x >= cmp)
{
sum += add;
ans += add*cnt;
cnt++;
add *= 10;
cmp *= 10;
}
ans += (x - sum)*cnt;
return ans;
} inline ll getval(ll x)
{
return (getans(x)-getans(m-1));
} ll binarysearch(ll l, ll r, ll val)
{
if (l == r)
{
while (getval(l) > val)
l--;
return l;
}
ll mid = (l + r) >> 1;
if (getval(mid) < val)
return binarysearch(mid+1, r, val);
else
return binarysearch(l, mid, val);
} int main()
{
while (cin >> w >> m >> k)
{
ll t = binarysearch(m, inf, w/k);// 二分的第三个参数必须这样,不能在其他地方乘,不然也会溢出
if (t < m)
puts("0");
else
cout << t - m + 1 << endl;
}
return 0;
}
Codeforces Round #219(Div. 2)373 B. Making Sequences is Fun(二分+找规律)的更多相关文章
- Codeforces Round #493 (Div. 2)D. Roman Digits 第一道打表找规律题目
D. Roman Digits time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- 数学 Codeforces Round #219 (Div. 2) B. Making Sequences is Fun
题目传送门 /* 数学:这题一直WA在13组上,看了数据才知道是计算cost时超long long了 另外不足一个区间的直接计算个数就可以了 */ #include <cstdio> #i ...
- DP Codeforces Round #FF (Div. 1) A. DZY Loves Sequences
题目传送门 /* DP:先用l,r数组记录前缀后缀上升长度,最大值会在三种情况中产生: 1. a[i-1] + 1 < a[i+1],可以改a[i],那么值为l[i-1] + r[i+1] + ...
- Codeforces Round #381 (Div. 2) D. Alyona and a tree 树上二分+前缀和思想
题目链接: http://codeforces.com/contest/740/problem/D D. Alyona and a tree time limit per test2 secondsm ...
- Codeforces Round #219 (Div. 2) E. Watching Fireworks is Fun
http://codeforces.com/contest/373/problem/E E. Watching Fireworks is Fun time limit per test 4 secon ...
- Codeforces Round #219 (Div. 1)(完全)
戳我看题目 A:给你n个数,要求尽可能多的找出匹配,如果两个数匹配,则ai*2 <= aj 排序,从中间切断,分成相等的两半后,对于较大的那一半,从大到小遍历,对于每个数在左边那组找到最大的满足 ...
- Codeforces Round #219 (Div. 1) C. Watching Fireworks is Fun
C. Watching Fireworks is Fun time limit per test 4 seconds memory limit per test 256 megabytes input ...
- Codeforces Round #219 (Div. 2) B. Making Sequences is Fun
B. Making Sequences is Fun time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- Codeforces Round #219 (Div. 2) D. Counting Rectangles is Fun 四维前缀和
D. Counting Rectangles is Fun time limit per test 4 seconds memory limit per test 256 megabytes inpu ...
随机推荐
- 【过时】update progress has encountered a problem解决办法
笔者第二次整理博客,已经抛弃MyEclipse了,我将公司项目转换成了idea的目录结构后大家都改换Idea进行开发,虽然我个人比较喜欢eclipse的简洁干净,但是Idea的确有很多方便开发的新功能 ...
- Linux下编译PHP常见错误及解决方法
1.configure: error: xml2-config not found. Please check your libxml2 installation.yum install libxml ...
- 农夫过河 (BFS)(队列)
1 .问题描述 要求设计实现农夫过河问题(农夫带着一只狼,一只养,一棵白菜,一次只能带一个东西)如何安全过河. 2 .问题的解决方案: 可以用栈与队列.深度优先搜索算法及广度优先搜索算法相应的原理去解 ...
- 2018.12.1 万圣节的小L
我回来啦 试题描述 今天是万圣节,小L同学开始了一年一度的讨要糖果游戏,但是在刚刚过去的比赛中小有成就的他打算给自己增加一点难度:如果没有讨到每一家的糖果就算输. 已知小L共有n(n不大于10000) ...
- Codeforces 755C:PolandBall and Forest(并查集)
http://codeforces.com/problemset/problem/755/C 题意:该图是类似于树,给出n个点,接下来p[i]表示在树上离 i 距离最远的 id 是p[i],如果距离相 ...
- Java线程池原理浅析
什么是线程池? 为了避免频繁重复的创建和销毁线程,我们可以让这些线程进行复用,在线程池中,总会有活跃的线程在占用,但是线程池中也会存在没有占用的线程,这些线程处于空闲状态,当有任务的时候会从池子里面拿 ...
- os.path.dirname(__file__)
os.path.dirname(__file__) 返回脚本的路径 描述: 必须实际存在的.py文件,如果直接在命令行执行,则会引发异常NameError: name 'file' is not de ...
- kuangbin专题 专题一 简单搜索 Dungeon Master POJ - 2251
题目链接:https://vjudge.net/problem/POJ-2251 题意:简单的三维地图 思路:直接上代码... #include <iostream> #include & ...
- spark 源码分析之十三 -- SerializerManager剖析
对SerializerManager的说明: 它是为各种Spark组件配置序列化,压缩和加密的组件,包括自动选择用于shuffle的Serializer.spark中的数据在network IO 或 ...
- java截取避免空字符丢失
1. 场景描述 数据后端是Hbase等nosql数据库,返回的数据以逗号分隔,java后端获取数据后,需要新增组装数据后再返回给前端. 2. 问题解决 2.1 问题定位 本来用的java的split进 ...