纠结了很长时间,最后写出来了一段比较搓的代码,把样例和UVa论坛上的测试数据都过了,可就是WA……最后只能搜解题报告,才知道用排列组合可以相当巧妙的解出来。大概思想就是逐位考虑该位出现0的情况,具体就是用左边的排列数乘以右边的组合数,分该位为0和不为0两种情况考虑。
具体的看代码吧,参考了网上的解题报告,有不懂自行google吧。
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long LL;
LL m,n;
LL cal(LL x)
{
LL ans=0;
LL dec=1;
LL k=0;
while(x>=10)
{
LL c=x%10;
x/=10;
if(c) ans+=x*dec;
else ans+=(x-1)*dec+(k+1);
k+=c*dec;
dec*=10;
}
return ans;
}
int main()
{
freopen("in.txt","r",stdin);
while(cin>>m>>n)
{
if(m<0) break;
LL a1=cal(m-1);
if(m==0) a1--;
LL a2=cal(n);
cout<<a2-a1<<endl;
}
return 0;
}