zoukankan      html  css  js  c++  java
  • POJ 1745 【0/1 背包】

    题目链接:http://poj.org/problem?id=1745

    Divisibility
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 13431   Accepted: 4774

    Description

    Consider an arbitrary sequence of integers. One can place + or - operators between integers in the sequence, thus deriving different arithmetical expressions that evaluate to different values. Let us, for example, take the sequence: 17, 5, -21, 15. There are eight possible expressions: 17 + 5 + -21 + 15 = 16 
    17 + 5 + -21 - 15 = -14 
    17 + 5 - -21 + 15 = 58 
    17 + 5 - -21 - 15 = 28 
    17 - 5 + -21 + 15 = 6 
    17 - 5 + -21 - 15 = -24 
    17 - 5 - -21 + 15 = 48 
    17 - 5 - -21 - 15 = 18 
    We call the sequence of integers divisible by K if + or - operators can be placed between integers in the sequence in such way that resulting value is divisible by K. In the above example, the sequence is divisible by 7 (17+5+-21-15=-14) but is not divisible by 5. 

    You are to write a program that will determine divisibility of sequence of integers. 

    Input

    The first line of the input file contains two integers, N and K (1 <= N <= 10000, 2 <= K <= 100) separated by a space. 
    The second line contains a sequence of N integers separated by spaces. Each integer is not greater than 10000 by it's absolute value. 

    Output

    Write to the output file the word "Divisible" if given sequence of integers is divisible by K or "Not divisible" if it's not.

    Sample Input

    4 7
    17 5 -21 15

    Sample Output

    Divisible
    

    Source

    题目大意:给N个数字和一个K,把N个数字加加减减后得到的数字是否能整除K。

    大概思路:

    一、暴力,全排列,爆炸,out!

    二、0/1背包

    状态:bool dp[ i ][ x ], 长度为 i 的序列的加减结果模 K 的后的 X 为真或为假

    状态转移: dp[ i+1 ][ (((x-num [i+1])%K)+K)%K ]  =  dp[ i ][ x ]       减第 i+1 个数

          dp[ i+1 ][ (((x+num [i+1])%K)+K)%K ]  =  dp[ i ][ x ]      加第 i+1 个数

    AC code (1224k 360ms):

     1 ///POJ 1745 【0/1背包】
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <iostream>
     5 #include <algorithm>
     6 #define INF 0x3f3f3f3f
     7 using namespace std;
     8 
     9 const int MAXN = 1e4+10;
    10 const int MAXK = 111;
    11 
    12 int num[MAXN];
    13 bool dp[MAXN][MAXK];
    14 int N, K;
    15 
    16 void slv()
    17 {
    18     memset(dp, 0, sizeof(dp));
    19     dp[1][((num[1]%K)+K)%K] = true;
    20     for(int i = 1; i < N; i++)
    21     {
    22         for(int p = 0; p < K; p++)
    23         {
    24             if(dp[i][p])
    25             {
    26                 dp[i+1][(((p+num[i+1])%K)+K)%K] = true;
    27                 dp[i+1][(((p-num[i+1])%K)+K)%K] = true;
    28             }
    29         }
    30     }
    31     if(dp[N][0]) printf("Divisible
    ");
    32     else printf("Not divisible
    ");
    33 }
    34 
    35 int main()
    36 {
    37     scanf("%d%d", &N, &K);
    38     for(int i = 1; i <= N; i++)
    39     {
    40         scanf("%d", &num[i]);
    41     }
    42     slv();
    43     return 0;
    44 }
  • 相关阅读:
    记一则玄乎奇玄的ADG误删自救事件
    ORACLE 日常信息查询sql
    Linux脚本判断磁盘容量
    postgresql数据库创建触发器记录表修改时间
    centos7关闭防火墙
    centos7 安装mysql5.7(二进制tar包方式)
    Oracle11G RMAN-06214: Datafile Copy /u01/app/oracle/product/11.2.0/db_1/dbs/snapcf_cpbd.f
    SQLPlus中set命令
    oradehub命令
    记一报错解决:ORA-00845: MEMORY_TARGET not supported on this system
  • 原文地址:https://www.cnblogs.com/ymzjj/p/9380392.html
Copyright © 2011-2022 走看看