codeforces Div.2 899D Shovel Sale

来源:互联网 发布:nature数据库使用 编辑:程序博客网 时间:2024/06/05 02:30

大意:
给出一个数 n
求 1<= a , b <= n , 记 N = a +b
求末尾有最多连续的9的N,a,b 有多少(无序)组。两组是不同的当且仅当 有一个数不同。

样例解释:
input
14
output
9
hint:
In the example the maximum number of nines at the end of total cost of two shovels is one. :

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.

思路:
一个n确定的最多的连续的9数字是可以确定的,比如 n=300 可以确定构造出 x99¯¯¯¯¯ 而不会去考虑 x9¯¯¯¯
枚举x,并计数,就是答案。

对于一个数如 abcde¯¯¯¯¯¯¯¯ 我们考虑最大位,即是否 abcde¯¯¯¯¯¯¯¯> 50000
如果是,那么考虑构造 A=x99999¯¯¯¯¯¯¯¯¯¯
否则 A=x9999¯¯¯¯¯¯¯¯¯
枚举 x = 0->8
对于A 的种数为

1 A-12 A-2...(A-1)/2 (A+1)/2

共 (A-1)/2 种,但是受 n 的限制,可能取不到值
n 的区间为
(A+1)/2 <= n <=A-1
n>=A
n<(A+1)/2
三种情况分别计数即可。

ps. 0个9结尾也算

实现代码:

#include <bits/stdc++.h>using namespace std;typedef long long ll;typedef pair <int,int> pii;#define mem(s,t) memset(s,t,sizeof(s))#define D(v) cout<<#v<<" "<<v<<endl#define inf 0x3f3f3f3f#define pb push_back//#define LOCALconst int mod=1e9+7;const int MAXN =1e5+10;int main() {    ll n,N;    cin>>n;    if(n<5){        cout<<(n-1)*n/2<<endl;        return 0;    }    ll a[15],b[15];    a[0]=b[0]=1;    a[1]=10,b[1]=9;    for(int i=2;i<=12;i++){        a[i]=a[i-1]*10;        b[i]=b[i-1]*10+9;    }    N=n;    int cnt=0;    while(N){        N/=10;        cnt++;    }    ll ans=0;    if(n>=5*a[cnt-1]){        cnt++;    }    for(int i=0;i<=8;i++){        ll A=i*a[cnt-1]+b[cnt-1];        if((A+1)/2<=n && n<=A-1){            ans+=n-(A+1)/2+1;        }        else if(n>=A){            ans+=(A-1)/2;        }    }    cout<<ans<<endl;    return 0;}
原创粉丝点击