ZCMU—1374

1374: Greatest Naruto Army

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 80  Solved: 23
[Submit][Status][Web Board]

Description

ccc is a VERY VERY BIG fans of Naruto(漩涡鸣人). Naruto's perseverance and passion attract him a lot, so does Naruto's skill "Kagebunsin no jyutu"(影分身术). Actually, ccc knows more details about "Kagebunsin no jyutu" than most of us. When Naruto makes an new illusion(幻象), his Chakra will divide into two parts A and B, and increase Naruto's weariness value by absolutely value of (A - B). 
For example, at first Naruto have 5 Chakra, then he makes an illusion who has 2 Chakra. So he left 3 Chakra, and increase his weariness value by 1 (3 - 2). If he make a new illusion again and give him 2 Chakra, then he only left 1 Chakra and his weariness value changes to 2. Naruto cannot make a new illusion unless his Chakra is bigger than 1. What's more, Naruto's illusion can also use "Kagebunsin no jyutu" and the weariness they gains will also return to Naruto himself.

Now Naruto wants to make as many illusions as he can because Kakashi Sensei wants to teach him a new Ninjyutsu(忍术) and minimum his weariness value.

Input

The first line of input contains an integer T, indicating the number of test cases, then T lines follow , each line contains a positive integer N, indicating the number of Naruto's Chakra.
0 < N < 10000000;

Output

For each case ,output the minimum weariness value of Naruto when he makes the most illusions.

Sample Input

2
5
9

Sample Output

2
3

【分析】

题意:你有n点查克拉,每次分身可以任意分出m点查克拉给分身,但是本身必须留1点,一次分身会获得的疲劳值就是abs(m-(n-m)),每个分身跟本人一样可以分出分身,疲劳值都加给本人,问在分出最多分身的情况下,最小的疲劳值是多少
因为要求分出最多的分身,所以分身一定是n个,然后考虑疲劳值最小,最小肯定是当前这个分身有x点查克拉,肯定是对半分获得的疲劳值最小,所以没想太多直接
if (i%2==0) f[i]=f[i/2]*2;
else f[i]=f[i/2]+f[i/2+1]+1;
但是这里这个递推虽然是对的,但是可以发现并没有考虑留下1点给自己,但是这是可以证明是对的,因为当前这个人需要留下一点,那么必然会增加1点疲劳值,那么这一点的疲劳值可以把这一点查克拉放到下一层的分身中,让分身多一点查克拉,那么分身就会多一点疲劳值,加回来还是一样的,如果是偶数,就是把剩下的2点疲劳值各分一点给两个分身
【代码】
#include 
#include 
#include 
#include  
using namespace std;
long long f[1001000];

long long find(int x)
{
	if (x<=1000000) return f[x];
	if (x%2==0) return 2*find(x/2);
	else return find(x/2)+find(x/2+1)+1;
}

int main()
{
	memset(f,-1,sizeof(f));
	f[0]=f[1]=f[2]=0;
	for (int i=3;i<=1000000;i++)
		if (i%2==0)
			f[i]=f[i/2]+f[i/2];
		else f[i]=f[i/2]+f[i/2+1]+1; 
	int T_T;scanf("%d",&T_T);
	while (T_T--)
	{
		int n;scanf("%d",&n);
		printf("%lld\n",find(n));
	}
	return 0;
} 


你可能感兴趣的:(zcmu)