Ultraviolet A 10036 Problem C: divisibility

Source: Internet
Author: User

Question: Can I add a plus or minus sign between each adjacent two items of an integer sequence so that the final result can be divisible by a given integer k <= 100.

DP [I] [J] indicates that the number of I is taken as the Boolean value where the remainder K is J.

 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define maxn 20000 5 using namespace std; 6  7 int t; 8 int n,k; 9 int a[maxn];10 bool flag;11 bool dp[maxn][110];12 13 int main()14 {15     scanf("%d",&t);16     while(t--)17     {18         memset(dp,false,sizeof(dp));19         scanf("%d%d",&n,&k);20         for(int i=0; i<n; i++)21         {22             scanf("%d",&a[i]);23         }24         dp[0][abs(a[0])%k]=true;25         for(int i=1; i<n; i++)26         {27             for(int j=0; j<k; j++)28             {29                 if(dp[i-1][j])30                 {31                     dp[i][(j+k+abs(a[i]))%k]=true;32                     dp[i][((j+k-abs(a[i]))%k+k)%k]=true;33                 }34             }35         }36         if(dp[n-1][0]) printf("Divisible\n");37         else printf("Not divisible\n");38     }39     return 0;40 }
View code

 

Ultraviolet A 10036 Problem C: divisibility

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.