zoukankan      html  css  js  c++  java
  • Codeforces Round #452 (Div. 2) D. Shovel Sale

    D. Shovel Sale
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    There are n shovels in Polycarp's shop. The i-th shovel costs i burles, that is, the first shovel costs 1 burle, the second shovel costs 2burles, the third shovel costs 3 burles, and so on. Polycarps wants to sell shovels in pairs.

    Visitors are more likely to buy a pair of shovels if their total cost ends with several 9s. Because of this, Polycarp wants to choose a pair of shovels to sell in such a way that the sum of their costs ends with maximum possible number of nines. For example, if he chooses shovels with costs 12345 and 37454, their total cost is 49799, it ends with two nines.

    You are to compute the number of pairs of shovels such that their total cost ends with maximum possible number of nines. Two pairs are considered different if there is a shovel presented in one pair, but not in the other.

    Input

    The first line contains a single integer n (2 ≤ n ≤ 109) — the number of shovels in Polycarp's shop.

    Output

    Print the number of pairs of shovels such that their total cost ends with maximum possible number of nines.

    Note that it is possible that the largest number of 9s at the end is 0, then you should count all such ways.

    It is guaranteed that for every n ≤ 109 the answer doesn't exceed 2·109.

    Examples
    input
    7
    output
    3
    input
    14
    output
    9
    input
    50
    output
    1
    Note

    In the first example the maximum possible number of nines at the end is one. Polycarp cah choose the following pairs of shovels for that purpose:

    • 2 and 7;
    • 3 and 6;
    • 4 and 5.

    In the second example the maximum number of nines at the end of total cost of two shovels is one. 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 to choose 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 is maximum possible for n = 50.

    有点像数位dp,但实际上不需要dp就能做。

    告诉你一个数组的长度n,其元素为1到n,你可以使任意两个数字相加,输出使最后连续为9的位数最长的情况数量。

    很明显,5*pow(10,n)为分界线:

    1-4:没有办法使末尾为9

    5-49:最后一位为9

    50-499:最后两位为9

    …………

    如果没有办法很快找出规律可以打一个表:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #include<stack>
    #include<map>
    #include<vector>
    #include<queue>
    using namespace std;
    const int MAXN=1e6+10;
    const double eps=1e-4;
    const int INF=1<<30;
    const int mod=1e9+7;
    #define ll long long
    #define edl putchar('
    ')
    #define useit  ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    #define FOR(i,a,b) for(int i=a;i<=b;i++)
    #define ROF(i,a,b) for(int i=a;i>=b;i--)
    #define mst(a) memset(a,0,sizeof(a))
    #define mstn(a,n) memset(a,n,sizeof(a))
    #define zero(x)(((x)>0?(x):-(x))<eps)
    int ans=0;
    int main()
    {
    	FOR(k,1,49)
    	{
    		ans=0;
    		FOR(i,1,k-1)
    		{
    			FOR(j,i+1,k)
    			if((i+j)%10==9) ans++;
    		}
    		cout<<k<<" "<<ans<<endl;
    	}
    	FOR(k,50,499)
    	{
    		ans=0;
    		FOR(i,1,k-1)
    		{
    			FOR(j,i+1,k)
    			if((i+j)%100==99) ans++;
    		}
    		cout<<k<<" "<<ans<<endl;
    	}
    }
    

    以区间5-49为例:

    5-14:每一个数字+1,最后一位为9时+1-1

    15-24:每一个数字+2,最后一位为9时+2-1

    25-34:每一个数字+3,最后一位为9时+3-1

    35-44:每一个数字+4,最后一位为9时+4-1

    45-49:每一个数字+5,最后一位为9时+5-1

    然后以区间50-499为例:

    50-149:每一个数字+1,最后两位为99时+1-1

    150-249:每一个数字+2,最后两位为99时+2-1

    250-349:每一个数字+3,最后两位为99时+3-1

    350-449:每一个数字+4,最后两位为99时+4-1

    450-499:每一个数字+5,最后两位为99时+5-1

    此外题目还有一个坑点,1-4的情况末尾是没有9的,但输出答案不是0,而是当前的组合数量,因为此时末尾无法有9,所以每一种情况都算。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #include<stack>
    #include<map>
    #include<vector>
    #include<queue>
    using namespace std;
    const int MAXN=1e5+10;
    const double eps=1e-4;
    const int INF=1<<30;
    #define mod 2147493647
    #define ll long long
    #define edl putchar('
    ')
    #define useit  ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    #define FOR(i,a,b) for(int i=a;i<=b;i++)
    #define ROF(i,a,b) for(int i=a;i>=b;i--)
    #define mst(a) memset(a,0,sizeof(a))
    #define mstn(a,n) memset(a,n,sizeof(a))
    #define zero(x)(((x)>0?(x):-(x))<eps)
    int n,m,k,l,flag=0;
    ll a[20],ans=0;
    int main()
    {
    	a[0]=5;
    	FOR(i,1,9)
    	a[i]=a[i-1]*10;
    	cin>>n;
    	{
    		ans=0;
    		k=0;
    		m=lower_bound(a,a+9,n)-a-1;//a[m]<n<a[m+1]
    		if(a[m+1]==n)
    		m++;
    		if(m<0)
    		cout<<n*(n-1)/2<<endl;
    		else
    		{
    			int p=pow(10,m+1);
    			k=(n+1-a[m])/p;
    			l=a[m];
    			ans+=p*(k+1)*k/2-k;
    			if(n>=k*p+l-1)
    			{
    				ans+=(k+1)*(n+1-k*p-l);
    				if(n>=(k+1)*p-1)ans--;
    			}
    			cout<<ans<<endl;
    		}
    	}
    }
    

      

  • 相关阅读:
    Spring IOC(二)
    Spring组件注册
    第六章:随机数和expect
    第二十一节:异常处理
    第二十节:基础知识阶段复习
    LVM逻辑卷管理
    第十九节:类的装饰器和数据描述符的应用
    第十八节:上下文管理协议
    第十七节:数据描述符
    第十六节:内置函数补充
  • 原文地址:https://www.cnblogs.com/qq936584671/p/8066774.html
Copyright © 2011-2022 走看看