POJ 1745 Divisibility (线性dp)

简介:
Divisibility
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions:10598   Accepted: 3787

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

Northeastern Europe 1999

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

题目大意:给n个数,让他们通过加减运算。推断结果可不可能被k整除

题目分析:用dp[i][j]表示通过前i个数的运算得到的余数为j可不可能。先看求a % k。假设a > k,则a = n * k + b。(n * k + b) % k == 0 + b % k = a % k,所以当a > k时,对求余数有影响的部分是不能被整除的部分。因此对于每一个数我们能够做a[i] = a[i] > 0 ? (a[i] % k) : -(a[i] % k)的预处理,然后就是在dp[i - 1][j]的情况下。推出下一状态。下一状态有两种可能,加和减,减的时候防止出现负数加上个k再取余,初始化dp[0][a[0]] = true最后仅仅要推断dp[n - 1][0]及前n个数通过加减运算是否能得到被k整除的值


#include <cstdio>
#include <cstring>
int const MAX = 10005;

bool dp[MAX][105];
int a[MAX];

int main()
{
    int n, k;
    while(scanf("%d %d", &n, &k) != EOF)
    {
        memset(dp, false, sizeof(dp));
        for(int i = 0; i < n; i++)
        {
            scanf("%d", &a[i]);
            a[i] = a[i] > 0 ? (a[i] % k) : -(a[i] % k);
        }
        dp[0][a[0]] = true;
        for(int i = 1; i < n; i++)
        {
            for(int j = 0; j <= k; j++)
            {
                if(dp[i - 1][j])
                {
                    dp[i][(j + a[i]) % k] = true;
                    dp[i][(k + j - a[i]) % k] = true;
                }
            }
        }
        printf("%s\n", dp[n - 1][0] ? "Divisible" : "Not divisible");
    }
}


版权声明:本文博客原创文章,博客,未经同意,不得转载。






本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/4749832.html,如需转载请自行联系原作者


相关文章
|
Java 应用服务中间件 Linux
Docker学习重点(7)~DockerFile
Docker学习重点(7)~DockerFile
1735 0
|
数据可视化 网络可视化
R语言混合图形模型MGM的网络可预测性分析
R语言混合图形模型MGM的网络可预测性分析
|
设计模式 消息中间件 Java
Java 设计模式:探索发布-订阅模式的原理与应用
【4月更文挑战第27天】发布-订阅模式是一种消息传递范式,被广泛用于构建松散耦合的系统。在 Java 中,这种模式允许多个对象监听和响应感兴趣的事件。
616 2
|
网络协议 Java 程序员
慧安-PLC4X学习
慧安-PLC4X学习
532 0
|
机器学习/深度学习 人工智能 自然语言处理
自然语言处理(一):RNN
自然语言处理(一):RNN
|
Linux Windows
svn 钩子 hooks 的 pre-commit 配置递交文件格式,文件大小,备注必填
svn 钩子 hooks 的 pre-commit 配置递交文件格式,文件大小,备注必填
|
机器学习/深度学习 编解码 监控
VDSR、DRRN、LapSRN、RCAN、DSRN…你都掌握了吗?一文总结超分辨率分析必备经典模型(二)(1)
VDSR、DRRN、LapSRN、RCAN、DSRN…你都掌握了吗?一文总结超分辨率分析必备经典模型(二)
646 0
|
存储 文字识别 搜索推荐
关于 Notion-Like 工具的反思和畅想
使用Notion-Like 工具的反思
739 0
关于 Notion-Like 工具的反思和畅想
|
存储 机器学习/深度学习 固态存储
展心展力 metaapp:基于 DeepRec 的稀疏模型训练实践
针对稀疏模型在分布式、图优化、算子、Runtime 等方面进行了深度的性能优化,并且完全开源。为metaapp取得了显著的性能提升和成本下降。
|
消息中间件 关系型数据库 MySQL
5. 消息队列中,如何保证消息的顺序性?
5. 消息队列中,如何保证消息的顺序性?
723 0
5. 消息队列中,如何保证消息的顺序性?

热门文章

最新文章