ACM Learning process-hdu2068 RPG's staggered (combinatorial math)

Source: Internet
Author: User

Description

This summer, Hangzhou Electric ACM Training Team first formed a female team, including a team called RPG, but as one of the training team members of the wild camel unexpectedly do not know RPG three person specifically who. RPG gave him the chance to let him guess, the first guess: R is the princess, p is the grass, G is the Moon hare; the second guess: R is grass, p is the Moon hare, G is the princess; Third guess: R is grass, p is princess, G is moon Hare; The poor wild camel finally divided the RPG for the sixth time. Because of the drive of the RPG, more and more women to do ACM, our wild camel want to know them, but now there are n many people, he has to guess the number of times, in order not to embarrass the wild camel, girls only ask him to correct half or above even if the clearance, how many groups of answers can make him smooth clearance.

Input

There are multiple case entries in the input data, each case containing an n, representing a few girls, (n<=25), n = 0 input end.

Output

Corresponds to the minimum moving distance for each set of data output.

Sample Input

120

Sample Output

11

Suppose I the person in his original position, the rest of the wrong row of the number is f[i], then the title required is all greater than or equal to (n+1)/2 f[i], and n+1 is to the odd number of even cases of unity.

Suppose the K-man is p[k],

Then it is n people first take out I person in their own position C (n, i), the rest of the wrong row p[n-i], and then control the range of I ok.

It is important to note that because the problem is not modeled, all the numbers can be very large, so it is best to recursive between adjacent items when seeking p and C.

Code:

#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<Set>#include<map>#include<queue>#include<string>#include<algorithm>#defineLL Long Longusing namespacestd;intN; LL p[ -], c[ -], ans;voidinit () {p[0] =1; p[1] =0; p[2] =1;  for(inti =3; I < -; ++i) p[i]= (I-1) * (p[i-1]+p[i-2]);}voidcal () {c[0] =1;  for(inti =1; I <= N; ++i) c[i]= c[i-1]* (n-i+1)/i;}voidWork () {ans=0; intHalf = (n+1)/2;  for(inti =0; I+half <= N; ++i) ans+ = c[i+half]*p[n-i-half]; printf ("%i64d\n", ans);}intMain () {//freopen ("test.in", "R", stdin);init ();  while(SCANF ("%d", &n)! = EOF &&N) {cal ();    Work (); }    return 0;}

ACM Learning process-hdu2068 RPG's staggered (combinatorial math)

Related Article

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.