UVa 12034 Race (recursive + combinatorial math)

Source: Internet
Author: User

Test instructions: A, a and two people race, ranking has three kinds of situation (tie first, ab,ba). Enter N to find the number of possible final positions for the individual race.

Analysis: Originally thought is the mathematics question, arranges the combination, later how to think also is not right. Turns out this is a recursive ...

The answer for the n person is f (n) assuming that the first name has I (0< i <= N) individuals, that is, C (n, I) species, and F (n-i) is possible, then easy.

F (n) =σc (n, i) f (n-i).

The code is as follows:

#include <iostream>#include<cstdio>#include<cstring>#defineMoD%10056using namespacestd;Const intMAXN =1001;intc[maxn+5][maxn+5], f[maxn+5];voidinit () { for(inti =0; i < MAXN; i++) C[i][i]=1, c[i][0] =1;  for(inti =1; I <1001; i++)         for(intj =1; J <= I; J + +) C[i][j]= (c[i-1][j-1] + c[i-1][j]) mod;}voidsolve () {f[0] =1, f[1] =1, f[2] =3;  for(inti =3; I <1001; i++)         for(intj =1; J <= I; J + +){            intK = (c[i][j]*f[i-j]) mod; F[i]= (k +f[i]) mod; }}intMain () {memset (c,0,sizeof(c)); Memset (F,0,sizeof(f));    Init ();    Solve (); intN, T, cases =0; CIN >>T;  while(t--) {scanf ("%d", &N); printf ("Case %d:%d\n", ++cases, f[n]); }    return 0;}

UVa 12034 Race (recursive + combinatorial math)

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.