Yongjun Luo → Algorithm Competition·Quick 300 Questions" Daily Question: "Hail Conjecture II"

[Title source]
http://oj.ecustacm.cn/problem.php?id=1744
http://oj.ecustacm.cn/viewnews.php?id=1023

[Title description]
The hail conjecture refers to the For an integer, if it is an odd number, multiply it by 3 and add 1, if it is an even number, divide it by 2, and finally it will become 1. No counterexample has been found yet.
For example the number 6. According to the above rules, it can become 3, 10, 5, 16, 8, 4, 2, 1, after 8 transformations.
Now
given a number n, find how many numbers exist that are transformed n times to get 1 .

[Input format]
Input a number n (0≤n≤55)

[Output format]
Output a number to indicate the answer.

【Input example】

样例1
0

样例2
4

样例3
8

【Example of output】

样例1
1

样例2
1

样例3
4

[Algorithm analysis]
This question obviously uses
backward calculation , and there are two situations:
(1) The backward calculation of "dividing by 2" is "multiplying by 2";
(2) The backward calculation of "multiplying by 3 plus 1" is "minus 1 Divided by 3", it should be noted that at this time, it should be judged that "minus 1 divided by 3" can be divided completely, and it is an odd number.

【Algorithm code】

#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
int n;
LL ans=0;
void dfs(LL x,LL cnt) {
    if(x==1 && cnt!=1)  return;
    if(cnt==n+1) {
        ans++;
        return;
    }
    dfs(2*x,cnt+1);
    if((x-1)%3==0 && ((x-1)/3)%2==1)
        dfs((x-1)/3,cnt+1);
}

int main() {
    cin>>n;
    dfs(1,1);
    cout<<ans;

    return 0;
}


/*
in:8

out:4
*/




[References]
https://blog.csdn.net/weixin_43914593/article/details/131768564








 

Guess you like

Origin blog.csdn.net/hnjzsyjyj/article/details/132372147