题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=4577
题目不解释了
c++用高精度太麻烦 所以xky大神在我一句一句说想法的时候用java写出来的
c++代码是
后来用了c++高精度 一直TLE 囧
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,k;
scanf("%d%d",&n,&k);
int sum=0;
int q=pow(2,k-1),p=q*2;
n=n/q;
sum=(n+1)/2;
while(n>0)
{
n=n/p;
sum=sum+(n+1)/2;
}
printf("%d\n",sum);
}
}
换成JAVA的高精度就是
import java.math.*;
import java.util.Scanner;
import java.io.*;
public class Main
{
public static void main(String[] args)
{
int T;
Scanner cin = new Scanner(System.in);
T=cin.nextInt();
while(T>0)
{
T--;
int k;
BigInteger n,one, two;
two=new BigInteger("2");
n=cin.nextBigInteger();
one=new BigInteger("1");
k=cin.nextInt();
int p=(int)Math.pow(2, k-1),q=p*2;
BigInteger temp=n.divide(new BigInteger(Integer.toString(p)));
BigInteger sum=new BigInteger("0");
temp=temp.add(one);
sum=temp.divide(two);
temp=temp.subtract(one);
while(temp.compareTo(new BigInteger("0"))==1)
{
temp=temp.divide(new BigInteger(Integer.toString(q)));
temp=temp.add(one);
sum=sum.add(temp.divide(two));
temp=temp.subtract(one);
}
System.out.println(sum);
}
}
}
JAVA 太好了 哈哈