How can I decrease the time complexity for this question?

回眸只為那壹抹淺笑 提交于 2019-12-20 07:17:27

问题


I am solving a problem such that for every given number between range (inclusive of boundary integers), I calculate the sum of the modified value of those numbers. For example,

x = 388,822,442
f(388,822,442)=3800200402

i.e; makes zeros for the identical digits. f(x) is modified value.

I iterated through each digit of an integer and made it zero if repeated.

 BufferedReader bf = new BufferedReader(newInputStreamReader(System.in));
        String s[], s1[];
        s = bf.readLine().trim().split("\\s+");
        s1 = bf.readLine().trim().split("\\s+");
        BigInteger sb = new BigInteger(s[1]);
        BigInteger sb1 = new BigInteger(s1[1]);
        BigInteger indexIncre = new BigInteger("1");
        BigInteger first = new BigInteger(s[1]);
        BigInteger last = new BigInteger(s1[1]);
        BigInteger length = last.subtract(first);
        BigInteger summation = new BigInteger("0");
         for (index = new BigInteger("0"); !index.subtract(length).toString().equals("1"); 
           index =index.add(indexIncre)) 
          {
            StringBuilder str = new StringBuilder(first.toString());
            int len = str.length();
            char c = str.charAt(0);


 for (int i = 1; i < len; i++) 
     {
            if (str.charAt(i) == c) {
                str.setCharAt(i, '0');
            } else
                c = str.charAt(i);
        }
            first = first.add(indexIncre);
            summation = summation.add(new BigInteger(str.toString()));

        }
        BigInteger modulo = BigInteger.valueOf((long) Math.pow(10, 9) + 7);
        System.out.println(summation.mod(modulo));

For example Input

1 8
2 12

output

49

This is in the form of

Input
NL L
NR R

The range of NL,L,NR,R are

1≤NL,NR≤10^5
1≤L≤R<10^100,000

Modified value is f(x)

f(8)=8,f(9)=9,f(10)=10,f(11)=10,f(12)=12

and modulo the sum of all these f(x) by 10^9+7

来源:https://stackoverflow.com/questions/57430181/how-can-i-decrease-the-time-complexity-for-this-question

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!