生活随笔
收集整理的這篇文章主要介紹了
HDU多校7 - 6853 Jogging(bfs+结论)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:點擊查看
題目大意:在二維平面中有一個點 ( x , y ) ,規定 “ 好點 ” 的定義是,gcd( x , y ) > 1 ,現在從點 ( x , y ) 開始,每一次都能等概率的選擇:
去周圍八個方向中的“好點”停留在原地不動
現在問在走無窮多次步數后,能夠從點 ( x , y ) 出發再回到點 ( x , y ) 的概率是多少
題目分析:比賽時稍微打了個表,感覺是暴力bfs出所有點然后計算答案,主要是答案不會計算,就放掉了,因為 1e12 以內的相鄰兩個素數之差最大也不過幾百,對應到二維平面中最多也就只有幾萬個點,所以可以暴力bfs出所有點
關于答案的計算,是一個結論,題解說的是“圖上隨機游走”算法,但我找不到相關博客去學習,無奈只能背過結論以防以后再遇到了
結論就是在建出無向圖后,答案就是 “起點的度數 + 1 ” 除以 “總度數 + n”
代碼:
?
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
#include<unordered_map>
using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=1e5+100;const int b[8][2]={0,1,0,-1,1,0,-1,0,1,1,-1,-1,-1,1,1,-1};set<pair<LL,LL>>vis;void bfs(LL x,LL y)
{int ans1=0,ans2=0;queue<pair<LL,LL>>q;q.emplace(x,y);vis.emplace(x,y);while(q.size()){tie(x,y)=q.front();q.pop();if(x==y)//如果遍歷到對角線的話,答案為0/1 {ans1=0,ans2=1;break;}ans2++;//統計有多少個點:ans2中的+n for(int i=0;i<8;i++){LL xx=x+b[i][0];LL yy=y+b[i][1];if(__gcd(xx,yy)==1)continue;ans2++;//記錄度數if(vis.count(make_pair(xx,yy)))continue;vis.emplace(xx,yy);q.emplace(xx,yy); }if(!ans1)//記錄(起點度數+1)的答案 ans1=ans2;}int gcd=__gcd(ans1,ans2);ans1/=gcd,ans2/=gcd;printf("%d/%d\n",ans1,ans2);
}int main()
{
#ifndef ONLINE_JUDGE
// freopen("data.in.txt","r",stdin);
// freopen("data.out.txt","w",stdout);
#endif
// ios::sync_with_stdio(false);int w;cin>>w;while(w--){LL x,y;scanf("%lld%lld",&x,&y);bfs(x,y);}return 0;
}
?
總結
以上是生活随笔為你收集整理的HDU多校7 - 6853 Jogging(bfs+结论)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。