Java 第n个数字,两个设置位n的范围为1到10^14
看下面的顺序:3,5,6,9,10,12,17,18,20 序列中的所有数字在其二进制表示中正好设置了2位。你的任务很简单,你必须找到这个序列的第n个数字。 对于每个测试用例,打印序列的第n个数字,用换行符分隔。由于数字可能非常大,请打印Java 第n个数字,两个设置位n的范围为1到10^14,java,Java,看下面的顺序:3,5,6,9,10,12,17,18,20 序列中的所有数字在其二进制表示中正好设置了2位。你的任务很简单,你必须找到这个序列的第n个数字。 对于每个测试用例,打印序列的第n个数字,用换行符分隔。由于数字可能非常大,请打印number%1000000007 我无法理解为什么某些测试用例会失败,我的代码在指定的范围内能否正常工作 以下是我的代码片段: public class Solution { public static void main(String[] args)
number%1000000007
我无法理解为什么某些测试用例会失败,我的代码在指定的范围内能否正常工作
以下是我的代码片段:
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner scan=new Scanner(System.in);
int t = scan.nextInt();
for(int j=0 ; j<t ; j++){
long n=scan.nextInt();
long r = 0;
long i = 1;
while((r = i*(i+1)/2) < n)
{
i++;
}
long res = (1<<i) + (1 << (i-(r-n)-1));
System.out.println(res);
}
}
}
公共类解决方案{
公共静态void main(字符串[]args){
/*在此处输入代码。从STDIN读取输入。将输出打印到STDOUT。您的类应命名为Solution*/
扫描仪扫描=新扫描仪(System.in);
int t=scan.nextInt();
对于(int j=0;j这是一个序列,因此您可以查看正确的百科全书,找到您需要的所有信息。
你也会发现算法。这里是其中一个算法的Java翻译
for (int j = 0; j < t; j++) {
long n = scan.nextInt();
if (n < 1954) {
nthLongTwoBitSets(n);
} else {
nthBigIntegerTwoBitSets(n);
}
}
Edit2:我无法访问该链接,但如果运行时错误是由于超时引起的,我们必须进行一些微优化(这在几乎所有情况下都是一种不好的做法),因此1)不再使用BigInteger,2)只使用一个System.out.println
,因为它非常昂贵。我们将字符串存储在
公共类解决方案{
专用最终静态长模=100000007L;
专用最终静态长固定=46480318;/(1L 1;
长i=n-((r*(r-1))>>>1)-1;
长rMod=1;
而(r>62){
r-=42;
rMod*=固定的;
rMod%=MOD;
}
rMod*=(1L 62){
i-=42;
iMod*=固定的;
iMod%=MOD;
}
iMod*=(1L请给出细节,这是不清楚的,澄清标题和帖子哪些测试用例失败了,您对代码、初始输入数据和预期输出的实际期望是什么?现在清楚了吗?不,您没有给出失败的时间和结果与预期输出的对比示例。这些是隐藏的测试用例…我不知道Emkumar:谢谢你的解决方案……但我还是得到了一些测试用例的错误答案和运行时错误…@premkumar你能提供一个链接到练习的陈述或更新你的问题副本粘贴它(整个陈述)吗?@premkumar现在就试试。我更新了答案,将算术移位改为仍然失败…我更新了我的问题…一次检查是否有任何线索…但我还是得到了一个运行时错误testcase@premkumar“打印编号%100000007”。供日后参考,请不要隐藏要求和限制。我已更新了答案。
public static void nthLongTwoBitSets(long n) {
long r = ((long) Math.sqrt((n << 3) - 1) + 1) >>> 1;
long i = n - ((r * (r - 1)) >>> 1) - 1;
long result = (1L << r) | (1L << i);
result %= 1000000007;
System.out.println(result);
}
public static void nthBigIntegerTwoBitSets(long n) {
int r = ((int) Math.sqrt((n << 3) - 1) + 1) >>> 1;
int i = (int) n - ((r * (r - 1)) >>> 1) - 1;
BigInteger result = BigInteger.ZERO.setBit(r).setBit(i).mod(MOD);
System.out.println(result.toString());
}
public class Solution {
private final static long MOD = 1000000007L;
private final static long FIXED = 46480318; // (1L << 42) % mod;
public static void main(String[] args) {
String sep = "\n";
StringBuilder sb = new StringBuilder(90000);
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
for(int j=0 ; j<t ; j++){
long n = scan.nextInt();
sb.append(nthLongTwoBitSets(n));
sb.append(sep);
}
sb.deleteCharAt(sb.length() - 1);
System.out.println(sb.toString());
}
public static long nthLongTwoBitSets2(final long n) {
long r = ((long) Math.sqrt((n << 3) - 1) + 1) >>> 1;
long i = n - ((r * (r - 1)) >>> 1) - 1;
long rMod = 1;
while (r > 62) {
r -= 42;
rMod *= FIXED;
rMod %= MOD;
}
rMod *= (1L << r);
rMod %= MOD;
long iMod = 1;
while (i > 62) {
i -= 42;
iMod *= FIXED;
iMod %= MOD;
}
iMod *= (1L << i);
iMod %= MOD;
final long result = (rMod + iMod) % MOD;
return result;
}
}