[ACM] sdut 2878 Circle (Gaussian deyuan), sdut2878

Source: Internet
Author: User

[ACM] sdut 2878 Circle (Gaussian deyuan), sdut2878

Circle Time Limit: 2000 ms Memory limit: 65536 K any questions? Click Here ^_^ Description: You have been given a circle from 0 to n rows-equal 1. if you are currently at x, you will move to (x release-interval 1) mod n or (x release + interval 1) mod n with equal probability. now we want to know the expected number of steps you need to reach x from 0. enter The first line contains one integer T-the number of test cases. each of the next T lines contains two integers n, divide x (0 rows ≤ limit x limit <limit n Limit ≤ limit 1000) as we mention above. output For each test case. print a single float number-the expected number of steps you need to reach x from 0. the figure is accurate to 4 decimal places. sample Input
33 25 410 5
Sample output
2.00004.000025.0000
Tip: 2014 Shandong fifth ACM College Student Program Design Competition


Solution:

N nodes numbered 0 to n-1 form a ring. Given a number x, calculate the expected number of steps from node 0 to node x. The probability of a node going to both sides is the same, and each step goes to one node.

Gaussian elimination element, n equations, n unknown numbers. set E [p] to the expected number of steps required to go from node p to node x. Then E [x] = 0;

For each node, E [p] = 0.5 * E [P-1] + 0.5 * E [p + 1] + 1, that is,-0.5 * E [P-1] + E [p]-0.5 * E [p + 1] = 1.

Code:

# Include <iostream> # include <cstdio> # include <cstring> # include <string. h >#include <cmath> # include <iomanip> # include <algorithm> using namespace std; // floating-point Gaussian elimination template const double eps = 1e-12; const int maxm = 1000; // m equations, n variables const int maxn = 1000; int m, n; double a [maxm] [maxn + 1]; /// Augmented Matrix bool free_x [maxn]; // determines whether it is an uncertain variable; double x [maxn]; // returns an int sign (double x) {return (x> eps)-(x <-eps);}/** return value:-1 no solution 0 has only one solution> = 1 has multiple solutions, according Free_x determines which solutions are uncertain */int Gauss () {int I, j; int row, col, max_r; m = n; // n equations, for (row = 0, col = 0; row <m & col <n; row ++, col ++) {max_r = row; for (I = row + 1; I <m; I ++) // find the maximum value of all rows in the current column (reduce the error when division is performed) {if (sign (fabs (a [I] [col])-fabs (a [max_r] [col])> 0) max_r = I;} if (max_r! = Row) {for (j = row; j <n + 1; j ++) swap (a [max_r] [j], a [row] [j]);} if (sign (a [row] [col]) = 0) // all the rows below the current column row are 0 (including row rows) {row --; continue ;} for (I = row + 1; I <m; I ++) {if (sign (a [I] [col]) = 0) continue; double tmp = a [I] [col]/a [row] [col]; for (j = col; j <n + 1; j ++) a [I] [j]-= a [row] [j] * tmp ;}} for (I = row; I <m; I ++) /// col = n 0... 0, a, no solution {if (sign (a [I] [col]) return-1;} if (row <n) // 0... 0, 0, there are multiple solutions, the number of free variable is n-row {for (I = row- 1; I> = 0; I --) {int free_num = 0; // The number of free variable elements int free_index; // The number of free variable elements for (j = 0; j <n; j ++) {if (sign (a [I] [j])! = 0 & free_x [j]) free_num ++, free_index = j;} if (free_num> 1) continue; // The number of uncertain variable values in this row exceeds 1, unable to solve, they are still uncertain variable elements // there is only one uncertain variable element free_index, which can be solved, and the variable is determined by double tmp = a [I] [n]; for (j = 0; j <n; j ++) {if (sign (a [I] [j])! = 0 & j! = Free_index) tmp-= a [I] [j] * x [j];} x [free_index] = tmp/a [I] [free_index]; free_x [free_index] = false;} return n-row;} // there is only one solution, and the strict upper triangle matrix (n = m) for (I = n-1; i> = 0; I --) {double tmp = a [I] [n]; for (j = I + 1; j <n; j ++) if (sign (a [I] [j])! = 0) tmp-= a [I] [j] * x [j]; x [I] = tmp/a [I] [I];} return 0 ;} /// template end int t, xx; int main () {cin> t; while (t --) {cin> n> xx; memset (a, 0, sizeof (a); for (int I = 0; I <n; I ++) {if (I = xx) {a [I] [I] = 1; a [I] [n] = 0; continue;} a [I] [I] = 1; a [I] [n] = 1; a [I] [(I-1 + n) % n] =-0.5; a [I] [(I + 1) % n] =-0.5;} Gauss (); cout <setiosflags (ios: fixed) <setprecision (4) <x [0] <endl;} return 0 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.