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 ...
随机推荐
- 在C#中用静态类来扩展类的方法
目录 在C#中用静态类来扩展类的方法 1.待扩展类 2.用于扩展的静态类中的静态方法 3 静态扩展方法的调用 4 适用场景 在C#中用静态类来扩展类的方法 1.待扩展类 private IList&l ...
- webpack-simple之vagrant热加载
"dev": "cross-env NODE_ENV=development webpack-dev-server --host 192.168.2.10 --port ...
- JavaScript面向对象之封装
Javascript是一种基于对象的语言,你遇到的所有东西几乎都是对象.但是,它又不是一种真正的面向对象编程语言,因为它的语法中没有 class(类). 那么,如果我们要把"属性" ...
- Kafka学习(一)-------- Quickstart
参考官网:http://kafka.apache.org/quickstart 一.下载Kafka 官网下载地址 http://kafka.apache.org/downloads 截至2019年7月 ...
- 新手如何学python?
在学习Python前,你应该规划好自己的学习方向,毕竟Python的方向比较多,比如: web开发 爬虫 自动化开发(运维,测试).脚本开发 大数据(数据挖掘,数据分析) 人工智能.机器学习 选择好自 ...
- POJ 2887:Big String(分块)
http://poj.org/problem?id=2887 题意:给出一个字符串,还有n个询问,第一种询问是给出一个位置p和字符c,要在位置p的前面插入c(如果p超过字符串长度,自动插在最后),第二 ...
- RabbitMQ windows 安装后 此时不应有 \RabbitMQ。
在安装过程中,如果细心实际上在安装的细节显示starting Service后就已经提示这个问题,实际上就是没有注册到RabbitMQ这个服务. 在研究了很久之后才发现原来是安装路径的问题,原先是安装 ...
- 看看大神 Paul Graham 对如何学习编程的回答
前言 我翻阅自己之前写的博客文章,发现在 2015 年我刚开始学习编程的时候,翻译了一段 Paul Graham 关于"How can I learn to program?"的回 ...
- c++ 组合
组合 题目描述 已知一个一维数组a1..n,又已知一整数m. 如能使数组a中任意几个元素之和等于m,则输出YES,反之则为NO. 输入 输入包括两行,第一行包含两个整数n m(1<=n<2 ...
- leetcode 141 Linked List Cycle Hash fast and slow pointer
Problem describe:https://leetcode.com/problems/linked-list-cycle/ Given a linked list, determine if ...