Multi-school 8-1010 HDU5389 (DP

Source: Internet
Author: User

Title: Everyone has 1~9 number, there are two doors, a bunch of people can enter, when and only if the number of these people and the number of the door with the same remainder. Ask how many ways to get these people to two doors.

Idea: The game when a bit of brain pumping, think for a long time to find is a simple dp .... Only need to count each number of people, and then dp[i][j] to represent the first I number of the person composed of J congruence of the scheme number, and then because each candidate 9 times the number is not affected, so each choice is actually selected X, x+9,x+18 ... , then you need to calculate the number of N to select X, x+9,x+18 ... The sum of the number of schemes, how do we calculate this? With a recursive c[n][k]=c[n][k-1]* (n-k)/(k+1) (the division is replaced with an inverse), all of these values can be obtained once in the complexity of O (n), and the calculation is added to the corresponding group.

#include <iostream>#include<map>#include<algorithm>#include<cstdio>#include<cstring>#include<cstdlib>#include<vector>#include<queue>#include<stack>#include<functional>#include<Set>#include<cmath>using namespacestd;#defineIOS Std::ios::sync_with_stdio (false); Std::cin.tie (0)#definePB Push_back#definePB Pop_back#defineFS First#defineSe Second#defineSq (x) (x) * (x)#defineEPS 0.00000001#defineIinf (1&LT;&LT;30)typedefLong LongLl;typedefLong LongLl;typedefLong LongLint;typedef pair<int,int>Pii;typedef pair<ll,ll>P;Const intMod=258280327;Const intmaxv=1e5+ -; inline ll Qpow (ll X,ll p) {ll ans=1;  while(p>0){        if(p&1) {ans= (ans*x)%MoD; } x= (x*x)%MoD; P>>=1; }    returnans;} ll INV[MAXV];voidinit () {inv[1]=1;  for(intI=2; i<maxv;i++) {Inv[i]=qpow (i,mod-2); }}ll dp[ the][ the];intcnt[ the];intT;intn,a,b;ll Tol=0; ll cc[Ten];intMain () {////////freopen ("/home/files/cppfiles/teamcontest/in", "R", stdin);init (); CIN>>T;  while(t--) {memset (DP,0,sizeofDP); memset (CNT,0,sizeofCNT); Tol=0; scanf ("%d%d%d",&n,&a,&b);  for(intI=0; i<n;i++){            intx; scanf ("%d",&x); CNT[X]++; Tol+=x; }        if((a+b)%9!=tol%9){            if(tol%9==a%9|| tol%9==b%9){                if(a==b) {cout<<2<<Endl; Continue; } cout<<1<<Endl; Continue; }Else{cout<<0<<Endl; Continue; }} dp[0][0]=1;  for(intI=1; i<=9; i++) {memset (CC,0,sizeofCC); ll Cn=1;  for(intk=0; k<=cnt[i];k++) {cc[k%9]= (cc[k%9]+CN)%MoD; Cn= (cn* (cnt[i]-k)%mod*inv[k+1])%MoD; }             for(intj=0; J<=min (Cnt[i],8); J + +){                 for(intk=0;k<9; k++) {dp[i][(k+I*J)%9]= (dp[i][(k+i*j)%9]+dp[i-1][k]* (Cc[j])%mod)%MoD; }            }        }        if(a==9) a=0; cout<<dp[9][a]<<Endl; }    return 0;}
View Code

Multi-school 8-1010 HDU5389 (DP

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.