[AGC057A] Antichain of Integer Strings 题解
QianRan_GG · · 题解
解题思路
本题考虑贪心做法。
首先可以想到位数越小的数作为其他数的子串的概率越大,所以我们可以从较大的数开始考虑。
当
当
1.
同理,当
还有就是
2.
AC 代码
#include <cstdio>
#include <algorithm>
#define int long long
//保险一点开了long long
using namespace std;
int lg[15]; // 用于快速求出n位的最小数即10的n - 1次方
signed main()
{
int t; scanf("%lld", &t);
//预处理
lg[1] = 1;
for(int i = 2; i <= 12; ++ i)
lg[i] = lg[i - 1] * 10;
for(int i = 1; i <= t; ++ i)
{
int l, r;
scanf("%lld%lld", &l, &r);
int x = l, y = r, s1 = 0, s2 = 0; //s1是l的位数,s2是r的位数
//求出位数
while(x) s1 ++ , x /= 10;
while(y) s2 ++ , y /= 10;
if(s1 == s2) printf("%lld\n", r - l + 1); //位数相同时
else
{
if(r / lg[s2] == 1) printf("%lld\n", r - max(r % lg[s2], max(r / 10, l - 1)));
//r最高位是1时 后s2 - 1位 前s2 - 1 位 l的值
else printf("%lld\n", r - lg[s2] + 1);
}
}
}