英文:
Big binary number
问题
I've translated the code part as you requested. Here it is:
public long FindBigNum(long n) {
System.out.println("");
System.out.println("Input: " + n);
StringBuilder binaryRepresentation = new StringBuilder();
for (Long i = 1l; i <= n; i++) {
System.out.println("Binary representation of " + i + " : " + Long.toBinaryString(i));
binaryRepresentation.append(Long.toBinaryString(i));
}
System.out.println("Concatenated string: " + binaryRepresentation.toString());
long longRepresentation = parseLong(binaryRepresentation.toString(), 2);
return longRepresentation;
}
public long parseLong(String s, int base) {
return new BigInteger(s, base).longValue();
}
If you have any specific questions or need further assistance with this code or the problem, please feel free to ask.
英文:
I'm trying to solve a problem, for the basic scenario it's working, nevertheless, I know that it is not optimal and has several faults.
Problem:
You are given a number n. Find the decimal value of the number formed by the concatenation of the binary representation of the first n positive integers. Print the answer 10^9 + 7.
Output format:
Print the answer modulo 10^9 + 7
Constraint:
1 <= n <= 10^9
Sample input: 3
Sample Output: 27
The binary representation of 1: 1
The binary representation of 2: 10
The binary representation of 3: 11
Concatenated string: 11011 -> Decimal representation is 27.
The first doubt that I have is: "modulo 10^9 + 7", I understand is the limit for the number of chars, but I don't know how to interpret it.
The second part of the question is with the solution:
Input: 20
Binary representation of 2 : 10.
Binary representation of 3 : 11
Binary representation of 4 : 100.
Binary representation of 5 : 101
Binary representation of 6 : 110
Binary representation of 7 : 111
Binary representation of 8 : 1000
Binary representation of 9 : 1001
Binary representation of 10 : 1010
Binary representation of 11 : 1011
Binary representation of 12 : 1100
Binary representation of 13 : 1101
Binary representation of 14 : 1110
Binary representation of 15 : 1111
Binary representation of 16 : 10000
Binary representation of 17 : 10001
Binary representation of 18 : 10010
Binary representation of 19 : 10011
The binary representation of 20: 10100
Concatenated string: 11011100101110111100010011010101111001101111011111000010001100101001110100
How would you recommend to tackle this kind of problem?
This is my current code:
public long FindBigNum(long n) {
System.out.println("");
System.out.println("Input: " + n);
StringBuilder binaryRepresentation = new StringBuilder();
for(Long i = 1l; i <= n; i++){
System.out.println("Binary representation of " + i + " : " + Long.toBinaryString(i));
binaryRepresentation.append(Long.toBinaryString(i));
}
System.out.println("Concatenated string: " + binaryRepresentation.toString());
//System.out.println("Binary representation: " + binaryRepresentation.toString());
long longRepresentation = parseLong(binaryRepresentation.toString(), 2);
//System.out.println("longRepresentation: " + l);
return longRepresentation;
}
public long parseLong(String s, int base){
return new BigInteger(s, base).longValue();
}
答案1
得分: 0
第一个疑问是:"模除 10^9 + 7",我理解这是字符数的限制,但我不知道如何解释它。
这意味着在除以该值时获取余数。这个值是1000000007
(我假设符号“^”表示指数)。
以下是我处理这个问题的方式。
- 像之前一样生成你的连接字符串。
- 使用
BigInteger
将二进制转换为十进制,然后使用BigInteger
的mod
方法取模。
这将最终非常慢且效率低下。但它可以作为一种控制,让你调查更优化的方法。也许可以使用StringBuilder并为其预分配存储空间。编写自己的整数到二进制转换程序等等。
这甚至可能需要更多的数学解决方案(例如,数论)而不是编程解决方案。
但你可以将结果与BigInteger解决方案进行比较,以验证它是否有效。
英文:
> The first doubt that I have is: "modulo 10^9 + 7", I understand is the limit for the number of chars, but I don't know how to interpret it.
That means to get the remainder when dividing by that value. It is the value 1000000007
(I am assuming the caret is exponentiation).
Here is how I would approach this.
- Generate your concatenated strings as you have been.
- Use
BigInteger
to convert from binary to decimal and then take themod
with the BigIntegermod
method.
That will ultimately be very slow and not efficient. However, it serves as a control to allow you to investigate optimal ways of doing it. Perhaps employing StringBuilder and pre-allocating storage for it. Writing your own int to binary conversion routine, etc.
It may even require more of a math solution (e.g. number theory) than a programming solution.
But you can compare your results to the BigInteger solution to verify if it is working.
答案2
得分: 0
以下是您提供的Java代码的翻译部分:
public class TestClass {
static long FindBigNum(long n) {
long result = 0;
StringBuilder binary = new StringBuilder();
long binarySumLong;
long moduloFactor = (long) (Math.pow(10, 9) + 7);
for (int number = 1; number < n; number++) {
System.out.println("Number is : " + number);
String binaryRepresentation = Long.toBinaryString(number);
System.out.println("Binary representation of " + number + " is " + binaryRepresentation);
binary.append(binaryRepresentation);
System.out.println("Value of StringBuffer binary is " + binary);
binarySumLong = binaryToInteger(binary.toString());
System.out.println("binarySumLong is : " + binarySumLong);
if (binarySumLong > (moduloFactor)) {
binarySumLong = binarySumLong % (moduloFactor);
binary = new StringBuilder(Long.toBinaryString(binarySumLong));
System.out.println(" *** binary after modification is : " + binary);
System.out.println(" *** and integer value of binary is : " + binaryToInteger(binary.toString()));
}
}
result = binaryToInteger(binary.toString());
return result;
}
public static int binaryToInteger(String binary) {
char[] numbers = binary.toCharArray();
int result = 0;
for (int i = numbers.length - 1; i >= 0; i--)
if (numbers[i] == '1')
result += Math.pow(2, (numbers.length - i - 1));
return result;
}
public static void main(String[] args) {
long output = FindBigNum((long) Math.pow(10, 4));
System.out.println("output is : " + output);
}
}
英文:
public class TestClass {
static long FindBigNum(long n) {
long result = 0;
StringBuilder binary = new StringBuilder();
long binarySumLong;
long moduloFactor = (long) (Math.pow(10, 9) + 7);
for (int number = 1; number < n; number++) {
System.out.println("Number is : " + number);
String binaryRepresentation = Long.toBinaryString(number);
System.out.println("Binary representation of " + number + " is " + binaryRepresentation);
binary.append(binaryRepresentation);
System.out.println("Value of StringBuffer binary is " + binary);
binarySumLong = binaryToInteger(binary.toString());
System.out.println("binarySumLong is : " + binarySumLong);
if (binarySumLong > (moduloFactor)) {
binarySumLong = binarySumLong % (moduloFactor);
binary = new StringBuilder(Long.toBinaryString(binarySumLong));
System.out.println(" *** binary after modification is : " + binary);
System.out.println(" *** and integer value of binary is : " + binaryToInteger(binary.toString()));
}
}
result = binaryToInteger(binary.toString());
return result;
}
public static int binaryToInteger(String binary) {
char[] numbers = binary.toCharArray();
int result = 0;
for (int i = numbers.length - 1; i >= 0; i--)
if (numbers[i] == '1')
result += Math.pow(2, (numbers.length - i - 1));
return result;
}
public static void main(String[] args) {
long output = FindBigNum((long) Math.pow(10, 4));
System.out.println("output is : " + output);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论