POJ1745Divisibility题解动态规划DP

747:Divisibility 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 v... 阅读详情
Divisibility
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 6514 Accepted: 2161

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

状态:
d[i][j]表示前i个数mod k=j是否可达
状态转移方程:
d[i][j]=1    if(d[i-1][((j-a[i])%m+m)%m]||d[i-1][((j+a[i])%m+m)%m])
所以:
d[i][j]=d[i-1][((j-a[i])%m+m)%m]+d[i-1][((j+a[i])%m+m)%m]

边界:

d[1][(a[1]%m+m)%m]=1

 

代码:


POJ 1745 / UVa 10036 Divisibility (同余定理,DP,经典题目) 题目链接: POJ http://poj.org/problem?id=1745 UVa OJ https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=977 注:两个OJ系统要求不同 ,UVa 需要键入测试组数,POJ 则没有。 阅读详情

相关推荐

dfs+记忆化

由于回溯过程中会产生大量的重复计算,需要通过记忆化数组来记录在某个状态下(s字符位置和t字符位置)的值来简化计算。我个人理解,其实直接动态规划就行以leetcode115为例。

qq_41413211的博客 233

POJ---1745 Divisibility【动态规划】

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

菜鸟 405

从零开始:使用Remix和MetaMask在测试网部署智能合约的完整指南

本文提供了一份完整的指南,教你如何使用Remix和MetaMask在测试网上部署智能合约。从环境搭建、获取测试网代币到编写和部署合约,再到与合约交互和连接Python后端,涵盖了智能合约开发的各个环节。特别适合初学者快速上手区块链开发,同时介绍了安全注意事项和调试技巧。

weixin_29019241的博客 476

POJ - 1745 - Divisibility 【动态规划题2】

POJ - 1745 - Divisibility 【动态规划题2】 Description Consider an arbitrary sequence of integers. One can place + or - operators between integers in the sequence, thus deriving different arithmetical express...

iimT的独立博客 328

PLC与变频器联动控制:从开关量、模拟量到通讯的接线与编程实战

在工业自动化系统中,电机调速控制是核心需求之一,其关键在于实现可编程逻辑控制器(PLC)与变频器之间的可靠联动。其基本原理是通过PLC输出控制信号,由变频器接收并驱动电机按设定速度运行。这项技术的核心价值在于实现电机的软启停、精确调速与节能控制,广泛应用于风机、水泵、传送带及精密张力控制等场景。其中,开关量控制以其接线简单、抗干扰能力强的特点,成为基础且可靠的方案;而模拟量控制则能实现无级平滑调速,满足更高精度的工艺要求。本文以西门子、三菱等主流品牌为例,深入解析了这两种主流控制方式的硬件接线、参数设置与梯

1278

SPH vs Lagrange网格混合建模实战:用LS-DYNA模拟弹丸穿孔效应

本文深入探讨了在LS-DYNA中运用SPH与Lagrange网格混合建模技术,高效模拟超高速碰撞下弹丸穿孔效应的实战方法。通过分析模型分区策略、耦合界面处理及关键参数设置,解决了传统方法在模拟大变形、材料断裂时的网格畸变或计算成本高昂问题,为相关工程仿真提供了兼顾精度与效率的解决方案。

weixin_29211309的博客 116

//dp动态规划//好题//poj1745//Divisibility

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,

lydia_ke的博客 424

分布式训练中All-Reduce、All-Gather、Reduce-Scatter原理

Reduce-Scatter = 先对所有进程的数据做 Reduce(如 sum / mean),再把结果“切分”分发给各进程。(是 Reduce + Scatter 的融合操作。

comli_cn的博客 347

POJ 1745 Divisibility 01背包变式

POJ 1745 Divisibility 题目大意 N个整数的序列(顺序固定),通过加或减的运算,结果能否被K整除 思路 用二维bool数组存余数情况 dp[N][K] 变量i :考察到第 i个数 变量j :第 j个余数的情况 dp[ i ][ j ]:前 i个数是否能运算出余数 j AC代码 #include <bits/stdc++.h> using namespace std;...

LiO_Liu的博客 248

k8s上分离集群seatunnel部署(生产推荐)

k8s上分离集群seatunnel部署(生产推荐)

寂夜了无痕的博客 896

倍福EL6002做RS232通信获取称重数据..

本文介绍EL6002模块通过RS232自由口通信协议获取从站数据。具体数据对照说明手册即可。

weixin_41883890的博客 1013

poj 1745 Divisibility

题目链接: http://poj.org/problem?id=1745 题意: 给出N个数a[i],在中间填上加号或减号,问能否出现一种填法使和能被K整除 题解: 我们可以 bool dp[i][j] = true 表示前i个数的和可能出现j,再通过dp[i][j] = true 当dp[i-1][j-a[i]] = true 或 dp[i-1][j+a[i]]

t51645的博客 317

Autosar EcuM配置-初始化及下电执行函数-基于ETAS软件

Autosar EcuM学习笔记-上电初始化执行函数及下电前执行函数,本文介绍在EcuM中的配置及使用。

赞的博客 858

POJ1745Divisibility【dp】

Divisibility Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11053   Accepted: 3953 Description Consider an arbitrary sequence of integ

走一步再走一步 734

华为OD新系统机试真题-8位LED控制器(C/C++/Py/Java/Js/Go)

华为OD新系统机试真题【8位LED控制器】,提供C语言、Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解,以及在线OJ模拟做题。华为OD新系统4月19号机试真题【8位LED控制器】

qq_45776114的博客 611

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

POJ1745-Divisibility

http://poj.org/problem?id=1745 #include<stdio.h> #include<string.h> int a[10001],n,m; bool dp[10001][101]; int main(void) { int i,j,t1,t2; scanf("%d%d",&n,&m); ...

weixin_30241919的博客 103

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

【DP】POJ-1745 Divisibility

给出n个有正有负的数字,在它们中间添加加号或者减号,问能否得到一个表达式的值除得尽K。

JSure的代码库 1171

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

POJ 1745 Divisibility

链接:http://poj.org/problem?id=1745 题意:给定N个数,问进行加减运算之后的结果是否能被K整除。   分析:DP。设布尔型数组dp[][],dp[i][j]表示对前i个数进行加减运算的结果模K的值为j。共N个阶段,每个阶段K个状态。 状态转移方程为:if(dp[i-1][j])      dp[i][abs(j+num[i])%k]=true;

iCpC之旅 893

poj 1745 Divisibility(DP + 数学)

poj 1745 Divisibility(DP + 数学)

1990

POJ-1745 Divisibility 【数塔模型】

J - DivisibilityTime Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld & %llu SubmitStatus Description Consider an arbitrary sequence of integers. One can place + or - operators between

DTL66的博客 446

POJ 1745 Divisibility

Divisibility Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & %llu Submit Status Description Consider an arbitrary sequence of integers. One can place + or - opera

※夏日星空※ 327

POJ 1745 Divisibility (DP)

Divisibility Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11452   Accepted: 4097 Description Consider an arbitrary sequence of integers. One can place + o

Calm微笑 441

poj 1745-Divisibility

http://poj.org/problem?id=1745 此题暴力明显没有前途,2的10000次方,此题也是DP,关键是要用到一个公式(a+b)%p=(a%p+b%p)%p,以及(a-b)%p=(a%p-b%p+p)%p,这里注意,要是括号的里是负数就不成立了,所以后面一个等式加了p。在读入数据的时候就可以先%p,再把是负数的加上p。每一次加一个数字就要%p,减一个就要加p再%p,跟18

glq007的acm的奋斗 1172

POJ 1745 Divisibility【DP】

Language:Default Divisibility Time Limit:1000MS Memory Limit:10000K Total Submissions:8396 Accepted:2909 Description Consider an arbitrary sequence of...

dianxuanzhi1019的博客 139

POJ 1745 Divisibility --dp

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 diffe...

lanshan1111的博客 158

POJ1745 Divisibility(dp)

Divisibility Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11053   Accepted: 3953 Description Consider an arbitrary sequence of integers. One can place +

GKHack's Blog 1121
上一篇: POJ3186Treats for the Cows题解动态规划DP
下一篇: POJ1651Multiplication Puzzle题解动态规划DP
power721
博客等级 码龄19年 176粉丝 135原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值