2020-06-04 13:40
湖南理工学院 Java 0 点赞 评论 收藏
分享
[留线请离言]:#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
typedef long long LL;
const double eps=1e-6;
LL qpow(LL x)
{
LL ans=1,base=10;
while(x)
{
if(x&1)
ans*=base;
base*=base;
x>>=1;
}
return ans;
}
LL get_len(LL x)
{
return floor(log10(x));
}
LL dfs(LL x) //0到x-1多少个2
{
// printf("x=%lld\n",x);
if(x<=2)
return 0;
else if(x<10)
return 1;
else
{
//对于x=3256000来说
LL len=get_len(x); //6
LL t=qpow(len); //1000000
LL f=x/t; //3
LL mod=x%t; // 256000
// printf("len=%lld,t=%lld,f=%lld,mod=%lld\n",len,t,f,mod);
if(mod) //此数包含不止一个非零数,如205或者3600或者200145000
return dfs(f*t)+dfs(mod)+(f==2)*mod;
else if(f==1) //此数是1000..000,只包含一个1和后面的0
return dfs(x/10)*10+x/10;
else //此数是200或者3000或者70000等等,只包含一个数字和后面的0
return dfs(t)*f+(f>2)*t;
}
}
LL solve(LL L,LL R)
{
return dfs(R+1)-dfs(L);
}
LL solve2(LL l,LL r)
{
LL i,k,ans=0;
for(i=l;i<=r;i++)
for(k=i;k>0;k/=10)
if(k%10==2)ans++;
return ans;
}
int main()
{
LL L,R;
while(cin>>L>>R)
{
cout<<solve(L,R)<<endl;
// cout<<solve2(L,R)<<endl;
}
// LL x;
// while(cin>>x)
// printf("dfs(%lld)=%lld\n",x,dfs(x));
// while(cin>>L>>R)
// for(LL i=1;i<=L;i++)
// for(LL j=i;j<=R;j++)
// {
// if(solve(i,j)!=solve2(i,j))
// printf("ERROR:\nsolve(%lld,%lld)=%lld\nsolve2(%lld,%lld)=%lld\n\n",i,j,solve(i,j),i,j,solve2(i,j));
// else
// printf("OK:L=%lld,R=%lld\n",i,j);
// }
return 0;
}
0 点赞 评论 收藏
分享
0 点赞 评论 收藏
分享
创作者周榜
更多
关注他的用户也关注了: