Title link: https://acm.zcmu.edu.cn/JudgeOnline/problem.php?id=1374
Topic
Ninja shadow clone. Ninjas have n-point chakras. When the number of chakras exceeds 1, they can make a shadow clone, create phantoms, and divide them ai-point chakras, leaving bi-point chakras, and at the same time increase the body fatigue value |ai-bi|, phantom And the subject can continue to shadow the clone until the number of chakras equals 1. Ask for the smallest fatigue value in the case of the most phantoms, and find the minimum fatigue value.
Range: (0 <N <1e7)
Ideas
dfs is solved recursively. First of all, he has n-1 phantoms. This is undoubtedly. Now we need to reduce the fatigue value, then reduce |ai-bi|, then when n is an even number, making ai equal to n/2 can increase the fatigue value by 0 , When n is odd, make ai equal to n/2, but the fatigue value is increased by 1. Since the phantom can also continue to be cloned, it can be solved recursively.
ac code
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int dfs(int n){
if(n <= 2) return 0;
if(n % 2 == 0) return dfs(n / 2) + dfs(n / 2);
else return dfs(n / 2) + dfs(n / 2 + 1) + 1;
}
int main(){
int t; scanf("%d", &t);
while(t --){
int n; scanf("%d", &n);
printf("%d\n", dfs(n));
}
return 0;
}