鏈接: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;
}
}