A Two Bases
After seeing the "ALL YOUR BASE ARE BELONG TO US" meme for the first time, numbers X and Y realised that they have different bases, which complicated their relations.
You're given a number X represented in base bx and a number Y represented in base by. Compare those two numbers.
Input
The first line of the input contains two space-separated integers n and bx (1 ≤ n ≤ 10, 2 ≤ bx ≤ 40), where n is the number of digits in the bx-based representation of X.
The second line contains n space-separated integers x1, x2, ..., xn (0 ≤ xi < bx) — the digits of X. They are given in the order from the most significant digit to the least significant one.
The following two lines describe Y in the same way: the third line contains two space-separated integers m and by(1 ≤ m ≤ 10, 2 ≤ by ≤ 40, bx ≠ by), where m is the number of digits in the by-based representation of Y, and the fourth line contains m space-separated integers y1, y2, ..., ym (0 ≤ yi < by) — the digits of Y.
There will be no leading zeroes. Both X and Y will be positive. All digits of both numbers are given in the standard decimal numeral system.
Output
Output a single character (quotes for clarity):
- '<' if X < Y
- '>' if X > Y
- '=' if X = Y
Sample test(s)
input
6 2
1 0 1 1 1 1
2 10
4 7
output
=
input
3 3
1 0 2
2 5
2 4
output
<
input
7 16
15 15 4 0 0 7 10
7 9
4 8 0 3 1 5 0
output
>
Note
In the first sample, X = 1011112 = 4710 = Y.
In the second sample, X = 1023 = 215 and Y = 245 = 1123, thus X < Y.
In the third sample, and Y = 48031509. We may notice that X starts with much larger digits andbx is much larger than by, so X is clearly larger than Y.
题目类型:暴力枚举
算法分析:直接将两个数转换成10进制然后比较
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
#pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7FFFFFFF; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e6 + 66; LL sa[166], sb[166]; int main() { // CFF; LL n, m, x, y; while (cin >> n >> x) { for (int i = 1; i <= n; i++) cin >> sa[i]; cin >> m >> y; for (int i = 1; i <= m; i++) cin >> sb[i]; LL ra = 0, rb = 0, tt; tt = 1; for (int i = n; i >= 1; i--) { ra += sa[i] * tt; tt *= x; } tt = 1; for (int i = m; i >= 1; i--) { rb += sb[i] * tt; tt *= y; } if (ra < rb) puts ("<"); else if (ra == rb) puts ("="); else puts (">"); } return 0; } |
B Approximating a Constant Range
When Xellos was doing a practice course in university, he once had to measure the intensity of an effect that slowly approached equilibrium. A good way to determine the equilibrium intensity would be choosing a sufficiently large number of consecutive data points that seems as constant as possible and taking their average. Of course, with the usual sizes of data, it's nothing challenging — but why not make a similar programming contest problem while we're at it?
You're given a sequence of n data points a1, ..., an. There aren't any big jumps between consecutive data points — for each 1 ≤ i < n, it's guaranteed that |ai + 1 - ai| ≤ 1.
A range [l, r] of data points is said to be almost constant if the difference between the largest and the smallest value in that range is at most 1. Formally, let M be the maximum and m the minimum value of ai for l ≤ i ≤ r; the range[l, r] is almost constant if M - m ≤ 1.
Find the length of the longest almost constant range.
Input
The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of data points.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 100 000).
Output
Print a single number — the maximum length of an almost constant range of the given sequence.
Sample test(s)
input
5
1 2 3 3 2
output
4
input
11
5 4 5 5 6 7 8 8 8 7 6
output
5
Note
In the first sample, the longest almost constant range is [2, 5]; its length (the number of data points in it) is 4.
In the second sample, there are three almost constant ranges of length 4: [1, 4], [6, 9] and [7, 10]; the only almost constant range of the maximum length 5 is [6, 10].
题目类型:区间最值查询
算法分析:使用两个指针pa和pb指向一个序列的头和尾,每次若新增的数使得当前的子序列合法,则pb指针自加1,否则使得pa指针自加1。在前面更新子序列的过程中还要更新最小值和最大值及其的位置,序列的最大值(答案)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
#pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7FFFFFFF; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e6 + 66; int aa[maxn]; int main() { // CFF; int n; while (scanf ("%d", &n) != EOF) { for (int i = 1; i <= n; i++) scanf ("%d", &aa[i]); int maxval = aa[1], minval = aa[1]; int maxval_pos = 1, minval_pos = 1, res = 1; int tt = 1, pa = 1, pb = 2; while (pa <= pb && pb <= n) { if (aa[pb] >= minval && aa[pb] <= maxval) { pb++; tt++; } else if (aa[pb] < minval) { if (abs (aa[pb] - maxval) <= 1) { minval = aa[pb]; minval_pos = pb; pb++; tt++; } else { if (maxval_pos == pa) { maxval = -INF; for (int i = pa + 1; i <= pb; i++) if (maxval < aa[i]) { maxval = aa[i]; maxval_pos = i; } tt--; pa++; } else { tt--; pa++; } } } else { if (abs (aa[pb] - minval) <= 1) { maxval = aa[pb]; maxval_pos = pb; pb++; tt++; } else { if (minval_pos == pa) { minval = INF; for (int i = pa + 1; i <= pb; i++) if (minval > aa[i]) { minval = aa[i]; minval_pos = i; } tt--; pa++; } else { tt--; pa++; } } } // cout << res << endl; res = max (res, tt); } printf ("%d\n", res); } return 0; } |
算法分析:另一种常数更小的方法是从左到右扫一遍,以当前数为基准向其左右扩展(满足相邻数字差最大为1),不断更新res。每次更新完之后,子序列的左指针直接跳到右指针所在的位置或者是其后一个位置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
#pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7FFFFFFF; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e6 + 66; int aa[maxn]; int main() { //CFF; int n; while (scanf ("%d", &n) != EOF) { for (int i = 1; i <= n; i++) scanf ("%d", &aa[i]); int pa = 1, res = 1; while (pa <= n) { int pb = pa; while (pb + 1 <= n && aa[pb+1] == aa[pa]) pb++; while (pa - 1 >= 1 && aa[pa-1] == aa[pa]) pa--; if (pb + 1 <= n && abs (aa[pb+1] - aa[pa]) == 1) { pb++; int tt = aa[pb]; while (pb + 1 <= n && (aa[pb+1] == tt || aa[pb+1] == aa[pa])) pb++; } res = max (res, pb - pa + 1); if (pa == pb) pa = pb + 1; else pa = pb; } printf ("%d\n", res); } return 0; } |
C The Two Routes
In Absurdistan, there are n towns (numbered 1 through n) and m bidirectional railways. There is also an absurdly simple road network — for each pair of different towns x and y, there is a bidirectional road between towns x and y if and only if there is no railway between them. Travelling to a different town using one railway or one road always takes exactly one hour.
A train and a bus leave town 1 at the same time. They both have the same destination, town n, and don't make any stops on the way (but they can wait in town n). The train can move only along railways and the bus can move only along roads.
You've been asked to plan out routes for the vehicles; each route can use any road/railway multiple times. One of the most important aspects to consider is safety — in order to avoid accidents at railway crossings, the train and the bus must not arrive at the same town (except town n) simultaneously.
Under these constraints, what is the minimum number of hours needed for both vehicles to reach town n (the maximum of arrival times of the bus and the train)? Note, that bus and train are not required to arrive to the town n at the same moment of time, but are allowed to do so.
Input
The first line of the input contains two integers n and m (2 ≤ n ≤ 400, 0 ≤ m ≤ n(n - 1) / 2) — the number of towns and the number of railways respectively.
Each of the next m lines contains two integers u and v, denoting a railway between towns u and v (1 ≤ u, v ≤ n,u ≠ v).
You may assume that there is at most one railway connecting any two towns.
Output
Output one integer — the smallest possible time of the later vehicle's arrival in town n. If it's impossible for at least one of the vehicles to reach town n, output - 1.
Sample test(s)
input
4 2
1 3
3 4
output
2
input
4 6
1 2
1 3
1 4
2 3
2 4
3 4
output
-1
input
5 5
4 2
3 5
4 5
5 1
1 2
output
3
Note
In the first sample, the train can take the route and the bus can take the route . Note that they can arrive at town 4 at the same time.
In the second sample, Absurdistan is ruled by railwaymen. There are no roads, so there's no way for the bus to reach town 4.
题目类型:BFS
算法分析:根据题意可知,火车道或者高速公路都一定会存在一个直接从1到n的边,所以根本就不会出现同时相撞的情况(除了n)。则直接判断通过两种交通工具是否能够同时从1到达n,不能的话直接输出-1,否则直接跑一个非直接连接1至n的线路的BFS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
#pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7FFFFFFF; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e6 + 66; int edge[466][466], n, m, res; bool vis[466]; struct node { int a, b, id; node (int aa, int bb, int dd) : a (aa), b (bb), id (dd) {} node () {} }; bool bfs (int x, int id) { res = 0; memset (vis, false, sizeof (vis)); vis[1] = true; queue <node> qu; qu.push (node (1, 0, id)); while (!qu.empty ()) { node tt = qu.front(); qu.pop (); if (tt.a == n) { res = tt.b; return true; } for (int i = 1; i <= n; i++) { if (tt.a != i && !vis[i] && edge[tt.a][i] == id) { qu.push (node (i, tt.b + 1, id)); vis[i] = true; } } } return false; } int main() { // CFF; while (scanf ("%d %d", &n, &m) != EOF) { memset (edge, 0, sizeof (edge)); for (int i = 1; i <= m; i++) { int u, v; scanf ("%d%d", &u, &v); edge[u][v] = edge[v][u] = 1; } for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { if (i == j) continue; if (edge[i][j] == 0) edge[i][j] = 2; } if (!bfs (1, 2) || !bfs (1, 1)) puts ("-1"); else { if (edge[1][n] == 1) bfs (1, 2); else bfs (1, 1); printf ("%d\n", res); } } return 0; } |
D Lipshitz Sequence
A function is called Lipschitz continuous if there is a real constant K such that the inequality|f(x) - f(y)| ≤ K·|x - y| holds for all . We'll deal with a more... discrete version of this term.
For an array , we define it's Lipschitz constant as follows:
- if n < 2,
- if n ≥ 2, over all 1 ≤ i < j ≤ n
In other words, is the smallest non-negative integer such that |h[i] - h[j]| ≤ L·|i - j| holds for all1 ≤ i, j ≤ n.
You are given an array of size n and q queries of the form [l, r]. For each query, consider the subarray ; determine the sum of Lipschitz constants of all subarrays of .
Input
The first line of the input contains two space-separated integers n and q (2 ≤ n ≤ 100 000 and 1 ≤ q ≤ 100) — the number of elements in array and the number of queries respectively.
The second line contains n space-separated integers ().
The following q lines describe queries. The i-th of those lines contains two space-separated integers li and ri(1 ≤ li < ri ≤ n).
Output
Print the answers to all queries in the order in which they are given in the input. For the i-th query, print one line containing a single integer — the sum of Lipschitz constants of all subarrays of .
Sample test(s)
input
10 4
1 5 2 9 1 3 4 2 1 7
2 4
3 8
7 10
1 9
output
17
82
23
210
input
7 6
5 7 7 4 6 6 2
1 2
2 3
2 6
1 7
4 7
3 5
output
2
0
22
59
16
8
Note
In the first query of the first sample, the Lipschitz constants of subarrays of with length at least 2 are:
The answer to the query is their sum.
题目类型:几何+区间最值查询
算法分析:首先一个隐含的条件是相邻两个点间的斜率要比跨过好多个点间的斜率要大,之后可以直接使用线段树查询每段的最值,这里使用一种更高效的做法:看每段的最值最终控制多长的区间,则最后的结果就是该最值乘上跨过这个段的子序列的个数,而控制区间的长度可以通过预处理得到
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
#pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7FFFFFFF; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e6 + 66; int aa[maxn], bb[maxn], Left[maxn], Right[maxn]; int main() { // CFF; int n, q; while (scanf ("%d %d", &n, &q) != EOF) { for (int i = 1; i <= n; i++) scanf("%d", &aa[i]); for (int i = 1; i <= n - 1; i++) bb[i] = abs (aa[i+1] - aa[i]); stack <PII> sta; sta.push (PII (0, INF)); for (int i = 1; i <= n - 1; i++) { while (!sta.empty () && bb[i] > sta.top().second) sta.pop (); Left[i] = sta.top ().first; sta.push (PII (i, bb[i])); } while (!sta.empty ()) sta.pop (); sta.push (PII (n, INF)); for (int i = n - 1; i >= 1; i--) { while (!sta.empty () && bb[i] >= sta.top ().second) sta.pop (); Right[i] = sta.top ().first; sta.push (PII (i, bb[i])); } int l, r; for (int i = 1; i <= q; i++) { scanf ("%d%d", &l, &r); LL res = 0; for (int j = l; j <= r - 1; j++) { int a = max (Left[j], l - 1); int b = min (Right[j], r); res += (LL) bb[j] * (j - a) * (b - j); } printf ("%I64d\n", res); } } return 0; } |
E Kleofáš and the n-thlon
Kleofáš is participating in an n-thlon - a tournament consisting of n different competitions in n different disciplines (numbered 1 through n). There are m participants in the n-thlon and each of them participates in all competitions.
In each of these n competitions, the participants are given ranks from 1 to m in such a way that no two participants are given the same rank - in other words, the ranks in each competition form a permutation of numbers from 1 to m. The score of a participant in a competition is equal to his/her rank in it.
The overall score of each participant is computed as the sum of that participant's scores in all competitions.
The overall rank of each participant is equal to 1 + k, where k is the number of participants with strictly smalleroverall score.
The n-thlon is over now, but the results haven't been published yet. Kleofáš still remembers his ranks in each particular competition; however, he doesn't remember anything about how well the other participants did. Therefore, Kleofáš would like to know his expected overall rank.
All competitors are equally good at each discipline, so all rankings (permutations of ranks of everyone except Kleofáš) in each competition are equiprobable.
Input
The first line of the input contains two space-separated integers n (1 ≤ n ≤ 100) and m (1 ≤ m ≤ 1000) — the number of competitions and the number of participants respectively.
Then, n lines follow. The i-th of them contains one integer xi (1 ≤ xi ≤ m) — the rank of Kleofáš in the i-th competition.
Output
Output a single real number – the expected overall rank of Kleofáš. Your answer will be considered correct if its relative or absolute error doesn't exceed 10 - 9.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .
Sample test(s)
input
4 10
2
1
2
1
output
1.0000000000000000
input
5 5
1
2
3
4
5
output
2.7500000000000000
input
3 6
2
4
2
output
1.6799999999999999
Note
In the first sample, Kleofáš has overall score 6. Nobody else can have overall score less than 6 (but it's possible for one other person to have overall score 6 as well), so his overall rank must be 1.
题目类型:概率DP
算法分析:简单来说就是用dp[i][j]表示经过前i个考试,一个人能够得到j分的概率。则状态转移方程为dp[i][j] = {Sigma(dp[i-1][k]) - dp[i-1][j-aa[i]]} / (m - 1),这里使用前缀和进行优化,详细求解过程可以参考:
http://blog.csdn.net/snowy_smile/article/details/50061111
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
#pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7FFFFFFF; const int MOD = 1e4 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e6 + 66; double dp[2][maxn]; double QQ (int flag, int val) { if (val < 0) return 0; else return dp[flag][val]; } int main() { // CFF; int n, m; while (scanf ("%d %d", &n, &m) != EOF) { for (int i = 0; i <= n * m; i++) dp[0][i] = 1; int score = 0, fir = 0, sec = 1; for (int i = 1; i <= n; i++) { int u; scanf ("%d", &u); score += u; for (int j = 0; j <= i - 1; j++) dp[sec][j] = 0; for (int j = i; j <= n * m; j++) { double tt = QQ (fir, j - 1) - QQ (fir, j - m - 1) - (QQ (fir, j - u) - QQ (fir, j - u - 1)); dp[sec][j] = dp[sec][j-1] + tt * 1.0 / (m - 1); } fir ^= 1, sec ^= 1; } if (m == 1) { puts ("1"); continue; } double res = dp[fir][score-1]; res = res * (m - 1) + 1; printf ("%.12lf\n", res); } return 0; } |
- « 上一篇:hdu2844
- hdu1011:下一篇 »