9 times to 1 numbers
瞿神的代码风格,典型的递归出口累加满足条件的个数。
对一个正整数作如下操作:如果是偶数则除以2,如果是奇数则加1,如此进行直到1时操作停止,求经过9次操作变为1的数有多少个?
这道题目似曾相识,但是 好像文法不同了。这道题目也应该是逆向思考,奇数只可能变偶数,偶数可能变奇数偶数,因此dfs搜索,到了cnt就累加
用map是为了避免重复搜索同一个数
编码方式的好博客
http://www.ruanyifeng.com/blog/2007/10/ascii_unicode_and_utf-8.html
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define pb push_back
#define CLR(a,x) memset(a,x,sizeof(a))
map<int,int>q;
LL ans;
void dfs(int cur,LL tmp)
{
if(cur==9/*||q[tmp]*/) {ans++;return;}
//q[tmp]=1;
if(tmp&1){
if(tmp*2!=1)
dfs(cur+1,tmp*2);
}else {
if(tmp*2!=1)
dfs(cur+1,tmp*2);
if(tmp-1!=1)
dfs(cur+1,tmp-1);
//ans+=2;
}
//return ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
ans=0;
q.clear();
dfs(0,1);
cout<<ans<<endl;
return 0;
}