其他分享
首页 > 其他分享> > 2017沈阳站 M (概率)

2017沈阳站 M (概率)

作者:互联网

https://vjudge.net/problem/HDU-6229
题意: N* N的网格,有k个障碍,格子编号从(0,0)到(n-1, n-1), G7人从00开始每一步等概率的走到相邻可走的格子,求在(x,y)格,x+y>=n -1的概率
思路:

#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false) ,cin.tie(0), cout.tie(0);
//#pragma GCC optimize(3,"Ofast","inline")
#define ll long long
#define PII pair<int, int>
//#define int long long
const int N = 2e5 + 5;
const int M = 1e5 + 5;
const int INF = 0x3f3f3f3f;
const ll LNF = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;
const double PI = acos(-1.0);
int dx[5] = {0, 0, 0, 1, -1};
int dy[5] = {0, 1, -1, 0, 0}; 
void solve() {
   int n, k; cin >> n >> k;
  vector<pair<int,int> > a(k);
   	set<PII> vis;
   	for (int i = 0; i < k; i++) {
   		int x, y;
   		cin >> x >> y;
   		vis.insert({x, y});
   		a[i] = {x, y};
   	} 

   int down = 0, up = 0;
   	down = (n - 2) * (n - 2) * 5 + (n - 2) * 4 * 4 + 3 * 4;
   	up = (n - 1) * 2 + (n - 2) * 3 + 4 + n * (n - 1) / 2 * 5 - (n - 2) * 2 - 2;
       
for ( auto i : vis ) {
       for ( int j = 0; j <5; ++ j ) {
           int nx = i.first + dx[j], ny = i.second + dy[j]; 
           if ( nx >= 0 && nx <= n-1 && ny >= 0 && ny <= n-1 ) {
               -- down; if ( nx + ny >= n -1 ) -- up;
               if ( vis.count({nx, ny}) == 0 ){
                   -- down; if ( i.second + i.first >= n - 1) -- up;
               }
           } 
       }

   }
   int d = __gcd(down, up);

   	down /= d, up /= d;
   	 cout << up << "/" << down << '\n';
}
int main() {
   IOS
   int t; cin >> t; int tt = 0;
   while( t -- ) {
       cout << "Case #" << ++tt << ": ";
       solve();
   }
   return 0;    
}

标签:概率,const,格子,int,up,down,--,2017,沈阳站
来源: https://www.cnblogs.com/muscletear/p/16462984.html