生活随笔
收集整理的這篇文章主要介紹了
HDU多校7 - 6853 Jogging(bfs+结论)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題目鏈接:點(diǎn)擊查看
題目大意:在二維平面中有一個(gè)點(diǎn) ( x , y ) ,規(guī)定 “ 好點(diǎn) ” 的定義是,gcd( x , y ) > 1 ,現(xiàn)在從點(diǎn) ( x , y ) 開始,每一次都能等概率的選擇:
去周圍八個(gè)方向中的“好點(diǎn)”停留在原地不動(dòng)
現(xiàn)在問在走無(wú)窮多次步數(shù)后,能夠從點(diǎn) ( x , y ) 出發(fā)再回到點(diǎn) ( x , y ) 的概率是多少
題目分析:比賽時(shí)稍微打了個(gè)表,感覺是暴力bfs出所有點(diǎn)然后計(jì)算答案,主要是答案不會(huì)計(jì)算,就放掉了,因?yàn)?1e12 以內(nèi)的相鄰兩個(gè)素?cái)?shù)之差最大也不過幾百,對(duì)應(yīng)到二維平面中最多也就只有幾萬(wàn)個(gè)點(diǎn),所以可以暴力bfs出所有點(diǎn)
關(guān)于答案的計(jì)算,是一個(gè)結(jié)論,題解說(shuō)的是“圖上隨機(jī)游走”算法,但我找不到相關(guān)博客去學(xué)習(xí),無(wú)奈只能背過結(jié)論以防以后再遇到了
結(jié)論就是在建出無(wú)向圖后,答案就是 “起點(diǎn)的度數(shù) + 1 ” 除以 “總度數(shù) + 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)//如果遍歷到對(duì)角線的話,答案為0/1 {ans1=0,ans2=1;break;}ans2++;//統(tǒng)計(jì)有多少個(gè)點(diǎn):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++;//記錄度數(shù)if(vis.count(make_pair(xx,yy)))continue;vis.emplace(xx,yy);q.emplace(xx,yy); }if(!ans1)//記錄(起點(diǎn)度數(shù)+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;
}
?
總結(jié)
以上是生活随笔為你收集整理的HDU多校7 - 6853 Jogging(bfs+结论)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。