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

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 则没有。 阅读详情

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:点击打开链接

题意:给出一些数,要求将所有的数都用上进行加减混合运算。如果结果能被k整除就输出Divisible,否则输出Not divisible

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#define MAXN 10005
using namespace std;

int dp[MAXN][105],a[MAXN];//dp[i][j]表示前i个数进行混合运算的结果%k是否为j,若是就为1,否则为0
int n,k;

int abs(int x)
{
    return x>0?x:-x;
}

int main(void)
{
    memset(dp,0,sizeof(dp));
    scanf("%d%d",&n,&k);
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&a[i]);
        a[i]=abs(a[i])%k;//为防止数太大,进行取模处理,并且全都变为正数
    }
    dp[1][a[1]%k]=1;
    for(int i=2; i<=n; i++)
        for(int j=0; j<k; j++)
            if(dp[i-1][j])
            {
                dp[i][abs(j+a[i])%k]=1;
                dp[i][abs(j-a[i])%k]=1;
            }
    if(dp[n][0])
        cout<<"Divisible"<<endl;
    else
        cout<<"Not divisible"<<endl;
    return 0;
}



北大POJ初级-动态规划 北大POJ初级-动态规划 解题报告+AC代码 立即下载

相关推荐

闲置服务器之代理服务器篇

这台服务器是在阿里云免费白嫖的,天工开物计划,阿里给的300块无门槛优惠券,买了一台新加坡的境外服务器,288,刚刚抵扣完,因为是境外服务器,能够访问一些国内不能访问的网站,所以本文章仅从技术的角度来搭建代理服务器,使用时,请自觉遵守国家相关规定,如有任何违法行为,与本人无任何关系!!!。本文使用tinyproxy来搭建代理服务器。

m0_50281408的博客 2115

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

高通SA8155P车载Camera开发避坑指南:从硬件拓扑到AIS服务调试全流程

本文详细解析了高通SA8155P车载Camera开发全流程,从硬件拓扑设计到AIS(Automotive Imaging System)服务调试,涵盖关键组件配置、软件架构剖析及高级调试技巧。通过实战经验分享,帮助开发者避开常见陷阱,优化系统性能,适用于智能驾驶系统的环境感知开发。

weixin_42550597的博客 333

//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

PyTorch实战:用FGSM攻击MNIST分类器(附可视化对比)

本文通过PyTorch实战演示了如何使用FGSM对抗攻击方法攻击MNIST手写数字分类器。文章详细解析了FGSM攻击原理,提供了从环境搭建、模型准备到攻击实施、可视化对比的完整代码实战流程,并探讨了对抗样本的生成与模型鲁棒性评估。

3a9bq4r8t2y的博客 220

桌面云虚拟机Ubuntu离线安装open-vm-tools

公司办公在桌面云中进行开发,桌面云是无法访问外网的,然后又有安装虚拟机的需求,使用VMware自带的VMware-tools安装功能失败,报错也需要联网才能解决,如果不安装VMware-tools,则虚拟机无法使用共享文件夹功能以及与本机共享剪贴板功能,非常难受,故自己研究解决如何离线安装open-vm-tools(open-vm-tools 是 VMware Tools 的开源实施)打开Ubuntu,可以在文件中看到生成的ISO文件,点开,将四个软件包复制粘贴至桌面。

weixin_43397593的博客 5097

openjudge divisibility

747:Divisibility 描述 Consider an arbitrary sequence of integers. One can place + or - operatorsbetween integers in the sequence, thus deriving different arithmeticalexpressions that evaluate to diffe

clover_hxy的博客 707

揭秘Synopsys EDA中的AI黑科技:DSO.ai如何改变传统芯片设计流程

本文深入探讨了Synopsys EDA中的AI黑科技DSO.ai如何革新传统芯片设计流程。通过数据驱动的方法论、功耗优化革命和时序收敛智能加速,DSO.ai显著提升了设计效率和质量,实现了从经验驱动到AI驱动的范式转变。文章还提供了实战指南,帮助团队跨越AI EDA的实施鸿沟。

weixin_29296241的博客 533

poj 1745 Divisibility(01背包应用)

Divisibility Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7707   Accepted: 2644 Description

志当存高远 1725

DM9000有线网卡驱动编写

DM9000有线网卡驱动编写 1.DM9000简介   DM9000 是一款完全集成的、性价比高、引脚数少、带有通用处理器接口的单芯片快速以太网控制器。 自 带一个 10/100M PHY 和 4K 双字的 SRAM ,DM9000A 为适应各种处理器提供了 8 位、16 位数据接口访问 内部存储器,DM9000 拥有自动协商功能,DM9000 特性如下:   1.集成自适应 10/100M 收发器。   2.内置 16k 字节的 SRAM。   3.支持硬件帧校验。   4.兼容 3.3V 和 5.0V

weixin_44453694的博客 4679

企业数字化转型解决方案架构设计:一个五维全景视角

本文系统探讨了企业数字化转型的架构设计框架,提出五维协同模型:企业架构作为战略罗盘,业务架构聚焦价值创造,应用架构实现能力载体,数据架构提供智能燃料,技术架构奠定基础支撑。文章深入分析了各架构域的核心要素、设计原则及相互关系,并指出数字化转型需通过渐进路径,结合战略引领、技术赋能和组织变革,构建有机统一的架构体系。该框架为企业应对数字化复杂性提供了系统性方法论,强调从整体视角实现技术与业务的深度融合。

金牌架构师 1260

POJ 1745 Divisibility 动态规划

Divisibility Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1745 Description Consider an arbitrary sequence of integers. One

Running Boy的AC Dream 496

注意力机制模块:CA(坐标注意力)二次创新:融合深度可分离卷积,打造 CADS 模块

本文提出CADS模块,将坐标注意力(CA)与深度可分离卷积(DSC)深度融合,优化移动端AI部署效率。通过重构CA的计算范式,用深度卷积替代传统1×1卷积,在保持精度的同时显著降低计算开销。文章深入分析了CA的计算瓶颈和DSC的实际效率问题,详细阐述了CADS的设计原理与PyTorch实现,并提供了多组性能对比数据。该模块可直接应用于YOLO、UNet等主流模型,为移动端AI部署提供高效解决方案。

ZXSXJ的博客 102

POJ Divisibility 1745【动态规划】

Language: Default Divisibility Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11045   Accepted: 3950 Description Consider an arbitrary sequence of int

豫帝哥哥 1491

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

poj1745 Divisibility(动态规划经典题)

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

qq_35634181的博客 298

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

POJ 1745 Divisibility G++ 动态规划 背

N个数,使用加号和减号的组合连接,判断结果能否整除K。

woniupengpeng的专栏 342

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

POJ1745Divisibility题解动态规划DP

DivisibilityTime Limit: 1000MS Memory Limit: 10000KTotal Submissions: 6514 Accepted: 2161DescriptionConsider an arbitrary sequence of integers. One can place + or - operators between integers in the sequence, thus deriving different arithmetical expression

魔神翼 675

做过的DP集合

POJ1745Divisibility(dp)

twinslizzy的博客 413

POJ:1745 Divisibility(思维+动态规划DP)

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

zugofn的博客 520

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

动态规划推荐题目(持续更新)

一.基础DP(无需进行特别优化) 1.POJ 1745 Divisibility 2.POJ 1837 Balance 3.POJ 1088 滑雪 4.HDU 2845 Beans 5.POJ 1050 To the Max   二.进阶DP(需要进行必要优化)   (单调队列优化) 1.HDU 3401 Trade 2.UESTC 1685 我要长高 3.POJ 1821

iCpC之旅 1196

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

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

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

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

【poj1745】 Divisibility

http://poj.org/problem?id=1745 (题目链接)题意:给出n串数,可以在其两两之间添加+或-,判断是否存在某种方案使得出的表达式的答案可以整除k。Solution   水题一道,勉强算是dp吧。。   先把每个数都mod k,f[i][j]表示到第i个数是否存在一种方案使得答案能被j整除,转移很显然。复杂度O(nk)。代码:// poj1745 #include<alg

MashiroSky的博客 410
上一篇: POJ---3628 Bookshelf 2【01背包】
下一篇: POJ---3061 Subsequence【尺取法】
Acmer之家
博客等级 码龄10年 95粉丝 97原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值