标签:
从矩形地图中选三个点,使得A-B,B-C,C-A的曼哈顿距离和在给定的一个范围内,求多少种选法。
水题,很容易发现曼哈顿距离和是一个矩形的周长,枚举长和宽统计即可。
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define F first
#define S second
typedef long long LL;
typedef pair<int, int> pii;
const int M = 1e9 + 7;
const int N = 5000;
struct PatrolRoute {
int countRoutes(int X, int Y, int minT, int maxT) {
LL ans = 0;
for (int i = 2; i < X; ++i)
for (int j = 2; j < Y; ++j) {
if ((i + j) * 2 >= minT && (i + j) * 2 <= maxT) {
ans += 1ll * (X - i) * (Y - j) % M * (i - 1) % M * (j - 1) % M;
}
}
return ans * 6 % M;
}
};
给出
感觉这题很难QAQ,首先n=16可以想到状压Dp,但是状态很难想,看了别人的题解才会做。。
当
//状压Dp
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define F first
#define S second
typedef long long LL;
typedef pair<int, int> pii;
const int N = 16;
int small[51], same[51];
vector<double> ans;
double dp[N][1 << N];
double gao(int x, int mask, int l) {
if (dp[x][mask] != -1.0) return dp[x][mask];
double &t = dp[x][mask];
if (mask == (1 << x)) return t = 1.0;
t = 0.0;
int cnt = 0;
for (int i = 0; i < l; ++i) {
if ((small[i] & mask) > 0) ++cnt;
else if ((same[i] & mask) != mask) ++cnt, t += gao(x, mask & same[i], l);
}
t /= cnt;
return t;
}
struct StrangeDictionary2 {
vector <double> getProbabilities(vector <string> words) {
int n = words.size(), l = words[0].size();
for (int i = 0; i < n; ++i)
for (int j = 0; j < 1 << n; ++j)
dp[i][j] = -1.0;
for (int i = 0; i < n; ++i) {
memset(small, 0, sizeof(small));
memset(same, 0, sizeof(same));
for (int k = 0; k < l; ++k)
for (int j = 0; j < n; ++j) {
if (words[j][k] < words[i][k]) small[k] |= 1 << j;
else if (words[j][k] == words[i][k]) same[k] |= 1 << j;
}
ans.pb(gao(i, (1 << n) - 1, l));
}
return ans;
}
};
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/mlzmlz95/article/details/46973971