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()
- 下一篇: mysql语句将日期转换为时间戳的方法