Codeforces 204A Little Elephant and Interval
http://codeforces.com/problemset/problem/204/A
题意:给定一个【L,R】区间,求这个区间里面首位和末尾相同的数字有多少个
思路:考虑这个问题满足区间加减,我们只考虑【1,n】,考虑位数小于n的位数的时候,我们枚举头尾的数是多少,然后乘上10的某幂次,再考虑位数相等时,从高位往低位走,先考虑头尾数字小于最高位的情况,也像刚才那个随便取,当头尾数字等于最高位时,从高往低走,先算不与这位相等的,走下一步就代表与这位相等。最后要记得判断一下如果原数字头等于尾,答案要加1.
#include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> #define ll long long ll st[200005]; ll solve(ll x){ if (x<=10){ return std::min(9LL,x); } if (x<=99){ ll t=x/10LL; return 9LL+t-1+(t<=(x%10)); } ll res=18; int top=0; while (x){ st[++top]=x%10; x/=10; } ll s=10; for (int i=1;i+3<=top;i++){ res+=9*s; s*=10LL; } for (int i=1;i<st[top];i++){ res+=s; } s/=10; for (int i=top-1;i>=2;i--) { for (int j=0;j<st[i];j++) res+=s; s/=10; } if (st[1]>=st[top]) res++; return res; } int main(){ ll n,m; scanf("%I64d%I64d",&n,&m); printf("%I64d\n",solve(m)-solve(n-1)); }