Codeforces 899D
来源:互联网 发布:eve手机辅助软件 编辑:程序博客网 时间:2024/06/06 12:35
D. Shovel Sale
time limit per test 1 second
memory limit per test 256 megabytes
There aren shovels in Polycarp's shop. The i-th shovel costs i burles, thatis, the first shovel costs 1 burle, thesecond shovel costs 2 burles, the thirdshovel costs 3 burles, and so on.Polycarps wants to sell shovels in pairs.
Visitors aremore likely to buy a pair of shovels if their total cost ends with several9s. Because of this, Polycarp wants to choose a pair of shovels to sell insuch a way that the sum of their costs ends with maximum possible number ofnines. For example, if he chooses shovels with costs12345 and 37454, their totalcost is 49799, it ends with two nines.
You are tocompute the number of pairs of shovels such that their total cost ends withmaximum possible number of nines. Two pairs are considered different if thereis a shovel presented in one pair, but not in the other.
Input
The first linecontains a single integern (2 ≤ n ≤ 109) — the number of shovels in Polycarp's shop.
Output
Print the numberof pairs of shovels such that their total cost ends with maximum possiblenumber of nines.
Note that it ispossible that the largest number of 9s at the end is 0, then you should countall such ways.
It is guaranteed that for everyn ≤ 109 the answer doesn't exceed 2·109.
Examples
Input
7
Output
3
Input
14
Output
9
Input
50
Output
1
Note
In the firstexample the maximum possible number of nines at the end is one. Polycarp cahchoose the following pairs of shovels for that purpose:
· 2 and 7;
· 3 and 6;
· 4 and 5.
In the secondexample the maximum number of nines at the end of total cost of two shovels isone. The following pairs of shovels suit Polycarp:
· 1 and 8;
· 2 and 7;
· 3 and 6;
· 4 and 5;
· 5 and 14;
· 6 and 13;
· 7 and 12;
· 8 and 11;
· 9 and 10.
In the third example it is necessary tochoose shovels 49 and 50, because the sum of their cost is 99, that means that the total number of nines is equal to two, which ismaximum possible forn = 50.
【题意】
现在给你1~n共n个数,现在你首先需要得到去两个数相加它们结尾9个数最多为多少(x),然后再找出去两个数使得它们相加和末位有x个0的方案数。
【思路】
我们先找出对于一个给定的n,其末位最多有几个9.
然后举个例子,如x=2,那么我们只要去枚举结尾有两个2的数字作为和,如99,199,299,399,499,599....899,然后只要去判断下哪些组合满足条件即可,具体见代码。
【PS】注意n<=4的特殊情况,应该把所有方案数输出。
#include <cstdio>#include <cmath>#include <map>#include <cstring>#include <algorithm>using namespace std;#define mst(a,b) memset((a),(b),sizeof(a))#define rush() int T;scanf("%d",&T);while(T--)typedef long long ll;const int maxn = 1000005;const ll mod = 1e9+7;const ll INF = 0x3f3f3f3f;const double eps = 1e-9;ll n;ll a[maxn];void init(){ a[1]=9; for(int i=2;i<=12;i++) a[i]=a[i-1]*10+9;}ll power(ll x, ll n){ ll ans=1; while(n) { if(n&1) ans*=x; x*=x; n>>=1; } return ans;}ll solve(int x,int pos){ ll cnt=x*power(10,pos)+a[pos]; //枚举结尾有pos个9的数 ll l=cnt-n,r=min(n,cnt-1); if(l<=0) l=1; if(l<=r) return(r-l+1)/2; return 0;}int main(){ init(); while(~scanf("%I64d",&n)) { if(n<=4) { printf("%I64d\n",(n-1)*n/2); continue; } else { int pos; for(int i=1;i<12;i++) { if(n*2<a[i]) //举个例子:n为50,最大为49+50,pos=2 { pos=i-1; break; } } ll ans=0; for(int i=0;i<=8;i++) { ans+=solve(i,pos); } printf("%I64d\n",ans); } } return 0;}
- Codeforces 899D
- codeforces 899D. Shovel Sale
- Codeforces 899D Shovel Sale
- codeforces Div.2 899D Shovel Sale
- CodeForces 617D CodeForces 617D
- CodeForces 101D
- CodeForces 103D
- CodeForces 222D Olympiad
- codeforces 242d
- CodeForces 111D
- 【codeforces】3D
- CodeForces 127D Password
- codeforces 257D. Sum
- Codeforces 292D
- Codeforces 275D
- Codeforces 126D
- Codeforces 337d
- Codeforces D. Points
- MySQL数据库压缩包安装配置过程
- hdu6237 素因子
- ZK官方MVC原理详解
- Springboot+Mybatis+Pagehelper+Aop动态配置Oracle、Mysql数据源
- 跟着小甲鱼学python第一天
- Codeforces 899D
- 【除留余数法定义hash函数+线性探测法解决hash冲突】数据结构实验之查找七:线性之哈希表
- 深入理解JDK动态代理机制
- python输出结果后面带着一个“None”
- C/C++:scanf函数的黑科技
- servlet的生命周期
- 设计模式六大原则(3):依赖倒置原则
- 运输层——TCP协议
- 9.1 结合例9.6分析以下两个程序能否交换