Hdu 5389 Zero Escape (DP)

Source: Internet
Author: User

Title: http://acm.hdu.edu.cn/showproblem.php?pid=5389

Test instructions: Define the number of roots: ① add up each number to get a new number, ② repeat ① until the number only 1 bits. Given the number of n,a,b and n digits, the n number is divided into two parts, so that the number of the sum of the two parts is equal to the number of the sum of the other part of a and the program number of B.

Analysis: A number A of the root s= (A-1)%9+1, in order to facilitate direct use of s=a%9, of which 0 represents 9. The definition Dp[i][j] represents the number of the number of the previous I number of the root of the scheme of J. For the number of I can optionally also can not choose, then the state transfer equation is dp[i][j]=dp[i-1][j] (not selected), dp[i][(J+c[i])%9]+=dp[i-1][j] (optional). Special case is {I:1~n,sigma (C[i])}%9!= (a+b)%9, this situation can not divide the n number into a non-empty two parts satisfying the condition, need a special sentence.

Code:

#include <iostream> #include <cstdio> #include <cstring>using namespace std;const int maxn = 1e5+7; const int mod = 258280327;int dp[maxn][10],c[maxn];int main () {int ncase,n,a,b,i,j;scanf ("%d", &ncase); while ( ncase--) {scanf ("%d%d%d", &n,&a,&b), int sum=0;for (i=1;i<=n;i++) {scanf ("%d", &c[i]); sum+=c[i];} if (sum%9!= (a+b)%9) {if (a%9==sum%9 && b%9==sum%9) printf ("2\n"); else if (a%9==sum%9 | | b%9==sum%9) printf ("1\n"); elseprintf ("0\n"); continue;} Dp[0][0]=1;for (i=1;i<=n;i++) {for (j=0;j<=8;j++) dp[i][j]=dp[i-1][j];for (j=0;j<=8;j++) dp[i][(J+c[i])%9]= (dp[i][(J+c[i])%9]+dp[i-1][j])%mod;} printf ("%d\n", dp[n][a%9]);} return 0;}

Hdu 5389 Zero Escape (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.