Recursive ACdream1420 High Speed Trains

Source: Internet
Author: User

Portal: Click to open link

Test instructions: n points, each point is covered by at least one edge, and there are no self-loops and heavy edges. Ask how many kinds of situations

A very interesting recursion, in fact, can use the idea of the repulsion to consider the problem

Set F[n] As the number of cases of n points, then there will be a maximum of n (n-1)/2 edges, each of which is optional. If you do not consider conditions that do not satisfy the condition (that is, if some points are not covered by an edge), the probable number is POW (2,n* (n-1)/2), which is to consider whether each edge exists

So for the answer, not a bit not covered, and then we come to the enumeration point by the number of edges covered, set to K

When there are k points are covered, the number of K points is f[k], then this k point is which K, need to combine number C (n,k), so for k points are overwritten, the total number of classes that do not meet the condition is f[k]*c (n,k)

K can be enumerated from 1 to n-1, and then consider a special case where no point is covered by an edge, there are only 1 cases.

In summary, the answer is F[n]=pow (2,n* (n-1)/2)-segma (F[k]*c (n,k)) where 1<=k<=n-1

Import Java.util.scanner;import java.math.*;p ublic class Main {static final int MX = + 5;public static void Main (Strin G[] args) {Scanner cin = new Scanner (system.in); BigInteger zero = biginteger.valueof (0); BigInteger one = biginteger.valueof (1); BigInteger = biginteger.valueof (2); biginteger[][] C = new BIGINTEGER[MX][MX]; C[0][0] = one;for (int i = 1; i < MX; i++) {c[i][0] = c[i][i] = one;for (int j = 1; j < I; J + +) {C[i][j] = c[i-1][ J-1].add (C[i-1][j]);}} BigInteger f[] = new Biginteger[mx];f[1] = one;f[2] = one;for (int i = 3; i < MX; i++) {F[i] = Two.pow (i * (i-1)/2) . Subtract (one); for (int k = 2; k <= i-1; k++) {F[i] = F[i].subtract (c[i][k].multiply (f[k]));}} while (Cin.hasnext ()) {int n = cin.nextint (); System.out.println (F[n]);}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Recursive ACdream1420 High Speed Trains

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.