Codeforces 899D Shovel Sale (数学)

本文解析了CodeForces D.ShovelSale问题,介绍了一种算法来计算从n个铲子中选择两把铲子的方式,使得这两把铲子的价格总和以尽可能多的9结尾的方法数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.


题目链接:http://codeforces.com/problemset/problem/899/D

题意:输入一个n,求从中任选两个数,使得其尾部9的个数最多的数对有多少个。(也可能存在0个9)

思路:分析数据可以发现,<50,尾部1个9;<500,尾部2个9;<5000,尾部3个9......依次类推。可以发现,只需要判断当前的数与5*10^x做比较即可判断尾部最多有多少个9。

设当前输入n的时候一对数尾部最多有x个9,设r为尾部有x个9的各个可能取值,

r > 2*n-1 (即r  > n + (n-1)) break;

r < n+1    ans += r/2;   

r < 2*n    ans += (2*n-r+1)/2;

代码:

#include <bits/stdc++.h>
#define LL long long
using namespace std;

LL n,k,cnt,ans;

int main(){
    k = 5;
    scanf("%lld",&n);
    if(n < 5){
        printf("%d\n",n*(n-1)/2);
    }
    else{
        while(k * 10 <= n) k *= 10, cnt ++;
        LL temp = 1, num = 0;
        for(int i = 0; i <= cnt; i ++){
            num = num * 10 + 9;
            temp *= 10;
        }
        for(int i = 0; i <= 8; i ++){
            LL r = i * temp + num;
            if(r > 2*n-1) break;
            else if(r < n + 1) ans += r/2;
            else if(r < 2 * n) ans += (2*n-r+1)/2;
        }
        printf("%lld\n",ans);
    }
    return 0;
}



### Codeforces 1487D Problem Solution The problem described involves determining the maximum amount of a product that can be created from given quantities of ingredients under an idealized production process. For this specific case on Codeforces with problem number 1487D, while direct details about this exact question are not provided here, similar problems often involve resource allocation or limiting reagent type calculations. For instance, when faced with such constraints-based questions where multiple resources contribute to producing one unit of output but at different ratios, finding the bottleneck becomes crucial. In another context related to crafting items using various materials, it was determined that the formula `min(a[0],a[1],a[2]/2,a[3]/7,a[4]/4)` could represent how these limits interact[^1]. However, applying this directly without knowing specifics like what each array element represents in relation to the actual requirements for creating "philosophical stones" as mentioned would require adjustments based upon the precise conditions outlined within 1487D itself. To solve or discuss solutions effectively regarding Codeforces' challenge numbered 1487D: - Carefully read through all aspects presented by the contest organizers. - Identify which ingredient or component acts as the primary constraint towards achieving full capacity utilization. - Implement logic reflecting those relationships accurately; typically involving loops, conditionals, and possibly dynamic programming depending on complexity level required beyond simple minimum value determination across adjusted inputs. ```cpp #include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); for(int i=0;i<n;++i){ cin>>a[i]; } // Assuming indices correspond appropriately per problem statement's ratio requirement cout << min({a[0], a[1], a[2]/2LL, a[3]/7LL, a[4]/4LL}) << endl; } ``` --related questions-- 1. How does identifying bottlenecks help optimize algorithms solving constrained optimization problems? 2. What strategies should contestants adopt when translating mathematical formulas into code during competitive coding events? 3. Can you explain why understanding input-output relations is critical before implementing any algorithmic approach? 4. In what ways do prefix-suffix-middle frameworks enhance model training efficiency outside of just tokenization improvements? 5. Why might adjusting sample proportions specifically benefit models designed for tasks requiring both strong linguistic comprehension alongside logical reasoning skills?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值