[Miscellaneous Questions] CSUOJ1276 Counting Route Sequence

Source: Internet
Author: User

http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1276

Test instructions: Go from point 1th to point N (each side can only walk once, the number of sides between the two nodes must be odd)

How many ways are there in different order of the nodes (e.g. 1->2->3->4 and 1->3->2->4 for two kinds)

Method number Modulo 1000000007

This problem only needs to consider the number of methods of alternating adjacent two nodes and then recursively multiply them.

is: If you walk from 1 to 5

Just consider the number of alternate methods of 2 and 3 first: (Obviously the number of combinations related to the number of edges)

Then a similar consideration of 3, 4 alternating method number

And then all of them are multiplied.

Formula is

The formula is

Because the range of N and M is 1e5, it is required to take the mold and therefore requires m! (N-M)! The inverse element

If the direct for once and then to TMP do EX_GCD really tle ...

1     LL tmp=1, ans=1; 2      for (LL i=min (n, m); i>=1; i--) 3     {4         tmp= (tmp*i)%mod; 5         Ans= (ans* (n+1-i))%mod; 6     }

So you can pre-preprocess the factorial in 1e5.

1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <climits>5#include <cctype>6#include <cmath>7#include <string>8#include <sstream>9#include <iostream>Ten#include <algorithm> One#include <iomanip> A using namespacestd; -#include <queue> -#include <stack> the#include <vector> -#include <deque> -#include <Set> -#include <map> +typedefLong LongLL; -typedefLong DoubleLD; + Const DoublePi=acos (-1.0); A Const Doubleeps=1e-9; at #defineINF 0x3f3f3f - #defineLson L, M, rt<<1 - #defineRson m+1, R, rt<<1|1 -typedef pair<int,int>PI; -typedef pair<int, PI >PP; - #ifdef _WIN32 in #defineLLD "%i64d" - #else to #defineLLD "%lld" + #endif - //#pragma COMMENT (linker, "/stack:1024000000,1024000000") the //ll Quick (ll A, ll b) {ll ans=1;while (b) {if (b & 1) ans*=a;a=a*a;b>>=1;} return ans;} * //inline int Read () {char ch= "; int ans=0;while (ch< ' 0 ' | | ch> ' 9 ') Ch=getchar (); while (ch<= ' 9 ' && Ch >= ' 0 ') {ans=ans*10+ch-' 0 '; Ch=getchar ();} return ans;} $InlinevoidPrint (LL x) {printf (LLD, x);p UTS ("");}Panax Notoginseng //inline void Read (LL &ret) {char c;int sgn; LL bit=0.1;if (C=getchar (), c==eof) return, while (c!= '-' &&c!= '. && (c< ' 0 ' | | C> ' 9 ')) C=getchar (), sgn= (c== '-'), -1:1;ret= (c== '-')? 0: (c ' 0 '); while (C=getchar (), c>= ' 0 ' &&c<= ' 9 ') ret=ret*10+ (c ' 0 '); if (c== "| | c== ' \ n ') {ret*=sgn; return;} while (C=getchar (), c>= ' 0 ' &&c<= ' 9 ') ret+= (c ' 0 ') *bit,bit/=10;ret*=sgn;} -  the Const intMod=1000000007; + inta[100005]; ALL jc[200005]; the voidPre () + { -jc[0]=1; $      for(intI=1; i<=200000; i++) $Jc[i]= (i*jc[i-1])%MoD; - } - voidEX_GCD (ll A, ll B, ll &x, LL &y) the { -     if(b)Wuyi     { theEX_GCD (b, a%b, x, y); -LL tmp=x; Wux=y; -Y=tmp-(A/b) *y; About     } $     Else -     { -x=1, y=0; -         return ; A     } + } the ll C (ll N, ll M) - { $     if(N==m | | m==0) the         return 1; the     if(m==1|| m==n-1) the         returnN; the //LL tmp=1, Ans=1; - //For (LL i=min (n, m); i>=1;i--) in //    { the //tmp= (tmp*i)%mod; the //ans= (ans* (n+1-i))%mod; About //    } the LL x, y; theEX_GCD (jc[m]*jc[n-m], mod, x, y); the     return(jc[n]*x)%MoD; + } - ll MOD (ll x) the {Bayi      while(x<0) thex+=MoD; the     returnX%MoD; - } - intMain () the { the pre (); the     intT; thescanf"%d", &t); -      while(t--) the     { the         intN; thescanf"%d", &n);94          for(intI=0; i<n-1; i++) thescanf"%d", &a[i]); theLL ans=1; the          for(intI=0; i<n-2; i++)98Ans= (Ans*c (a[i+1]-1)/2+ (a[i]-1)/2, (a[i]-1)/2)%mod)%MoD; About print (MOD (ans)); -     }101     return 0;102}
Csuoj 1276

[Miscellaneous Questions] CSUOJ1276 Counting Route Sequence

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.