首先,第一位最多可放置(n/(2^(k-1)+1)/2个(第奇数个肯定全都能放到),因为奇数不含因子2
然后再看多余部分,可以放置1,3,5,7乘以他们的2^(jk)(j=1,2,3,4,...),这个跑一个循环就出来了
两部分加和,即为答案
这题一开始卡超时,后来发现把2^k写到循环里面了,改到循环外面存一下就AC了
组队赛的第一个JAVA题
import java.util.Scanner;
import java.math.BigInteger;
public class Main {
public static void main(String args[]){
Scanner cin=new Scanner(System.in);
int T;
T=cin.nextInt();
for(int i=0;i<T;i++){
BigInteger n=cin.nextBigInteger();
int k=cin.nextInt();
BigInteger u,v;
BigInteger TWO=BigInteger.ONE.add(BigInteger.ONE);
BigInteger ans=n.divide(TWO.pow(k-1));
if(ans.mod(TWO).equals(BigInteger.ONE))
{
u=ans.divide(TWO).add(BigInteger.ONE);
}
else
{
u=ans.divide(TWO);
}
BigInteger pp=TWO.pow(k);
while(true)
{
ans=ans.divide(pp);
if(ans.equals(BigInteger.ZERO))break;
if(ans.mod(TWO).equals(BigInteger.ONE))
{
v=ans.divide(TWO).add(BigInteger.ONE);
}
else
{
v=ans.divide(TWO);
}
u=u.add(v);
}
System.out.println(u);
}
}
}