链接:https://ac.nowcoder.com/acm/contest/329/G
来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
经过了选号和漫长的等待,处女座终于拿到了给小姐姐定制的手环,小姐姐看到以后直呼666!
处女座其实也挺喜欢6这个数字的,实际上他做手环的时候选取的k=6。所以他对于包含数码6的数字极其敏感。每次看到像4567这样的数字的时候他的心就像触电了一样,想起了小姐姐。
现在你要给处女座展示一系列数字,你想知道他的内心会激动多少次。对于同一个数字,他最多只会激动一次,即如果这个数是66666,他还是只会激动一次。
输入描述:
一行包括两个数字l,r,表示你给处女座展示的数字范围为[l,r]。
输出描述:
一行一个整数,表示处女座内心激动的次数。
示例1
输入
复制
10 20
输出
复制
1
备注:
0
≤
l
≤
r
≤
10
18
思路:计算区间含6的个数,那么就要考虑1en个以内的不含6的个数,即每一位为012345789,所以共9^n个数。
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static BigInteger e=new BigInteger("10");
public static BigInteger f=new BigInteger("1");
public static BigInteger g=new BigInteger("0");
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
BigInteger l=sc.nextBigInteger();
BigInteger r=sc.nextBigInteger();
BigInteger tmp=new BigInteger("1");
l=l.subtract(tmp);
System.out.println(solve(r).subtract(solve(l)));
sc.close();
}
private static BigInteger solve(BigInteger r) {
// TODO Auto-generated method stub
BigInteger i;
BigInteger j=new BigInteger("9");
BigInteger k=new BigInteger("100");
if(r.compareTo(g)<=0)
return g;
BigInteger tmp=new BigInteger("0");
for(i=r.divide(e).multiply(e);i.compareTo(r)<=0;i=i.add(f))
if(check(i))
tmp=tmp.add(f);
return solve(r.divide(e).subtract(f)).multiply(j).add(tmp).add(r.divide(e));
}
private static boolean check(BigInteger i) {
// TODO Auto-generated method stub
BigInteger tmp=new BigInteger("6");
while(i.compareTo(g)>0){
if(i.mod(e).compareTo(tmp)==0)
return true;
i=i.divide(e);
}
return false;
}
}