首页 > 解决方案 > 如何降低这个问题的时间复杂度?

问题描述

我正在解决一个问题,即对于范围之间的每个给定数字(包括边界整数),我计算这些数字的修改值的总和。例如,

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

IE; 为相同的数字设置零。f(x) 是修改后的值。

我遍历整数的每个数字,如果重复则将其设为零。

 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));

例如 输入

1 8
2 12

输出

49

这是形式

输入
NL L
NR R

NL,L,NR,R 的范围是

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

修改后的值为f(x)

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

并取模所有这些的总和f(x) by 10^9+7

标签: javaloopsbiginteger

解决方案


推荐阅读