fzu1062 洗牌问题(思路模拟)
生活随笔
收集整理的这篇文章主要介绍了
fzu1062 洗牌问题(思路模拟)
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
http://acm.fzu.edu.cn/problem.php?pid=1062
一开始想暴力找规律,没看出来。。然后开始推,推测根据1再次返回第一个的时候顺序也复原,然后想以此推导出一个规律公式,发现很困难。
其实只要dfs模拟1的路径就好,单路线也不会超时。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<cstdlib> 6 #include<cmath> 7 #define lson l, m, rt<<1 8 #define rson m+1, r, rt<<1|1 9 #define IO ios::sync_with_stdio(false);cin.tie(0); 10 #define INF 1e9 11 typedef long long ll; 12 using namespace std; 13 int n, flag, cnt; 14 void dfs(int cur) 15 { 16 if(cur == 1){ 17 cout << cnt << endl; 18 return ; 19 } 20 cnt++; 21 if(cur<=n){ 22 dfs(cur*2); 23 } 24 else if(cur>n){ 25 dfs((cur-n)*2-1); 26 } 27 } 28 int main() 29 { 30 //记录1的位置即可 31 while(cin >> n){ 32 flag=0; 33 cnt=1; 34 dfs(2); 35 } 36 return 0; 37 }
转载于:https://www.cnblogs.com/Surprisezang/p/8878537.html
总结
以上是生活随笔为你收集整理的fzu1062 洗牌问题(思路模拟)的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 08-cmake语法-set()
- 下一篇: 任务和函数