POJ 1745 Divisibility(0,1背包)(好题)

题意: 输入n个有序数字,数字前可加上+或-,求是否存在这样的和,使得该和能够整除数字k

每个数字前只有取正或负两种情况,所以符合0,1背包,而且背包的重量是除k的余数(均是正数)

//172 KB	297 ms	C++	785 B
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
int num[10010];
bool dp[2][105];
int n,mod;

int main()
{
    while(~scanf("%d%d",&n,&mod))
    {
        memset(dp,0,sizeof(dp));
        dp[0][0]=true;
        for(int i=1;i<=n;i++) scanf("%d",&num[i]);
        for(int i=1;i<=n;i++)
        {
            memset(dp[i&1],0,sizeof(dp[i&1]));
            for(int j=0;j<mod;j++)
            {
                int t1=(j+num[i]+10000*mod)%mod;
                int t2=(j-num[i]+10000*mod)%mod;
                dp[i&1][t1]=max(dp[i&1][t1],dp[(i-1)&1][j]);
                dp[i&1][t2]=max(dp[i&1][t2],dp[(i-1)&1][j]);
            }
        }
       if(dp[n&1][0]) printf("Divisible\n");
       else printf("Not divisible\n");
    }
    return 0;
}
时间: 2024-08-28 10:38:56

POJ 1745 Divisibility(0,1背包)(好题)的相关文章

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 sequen

POJ 3624 Charm Bracelet(01背包裸题)

Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 38909   Accepted: 16862 Description Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the best charms possible fro

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 arithmet

poj 1745 Divisibility(DP + 数学)

题目链接:http://poj.org/problem?id=1745 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, f

poj 1745 Divisibility

Divisibility Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & %llu Description Consider an arbitrary sequence of integers. One can place + or - operators between integers in the sequence, thus deriving different arithmetical expr

poj 1745 Divisibility 【DP】

Divisibility Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11044   Accepted: 3949 Description Consider an arbitrary sequence of integers. One can place + or - operators between integers in the sequence, thus deriving different arithmet

poj 1745 Divisibility(DP)

最大子矩阵 Time Limit: 30000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description 给你一个m×n的整数矩阵,在上面找一个x×y的子矩阵,使子矩阵中所有元素的和最大. Input 输入数据的第一行为一个正整数T,表示有T组测试数据.每一组测试数据的第一行为四个正整数m,n,x,y(0<m,n<1000 AND 0<x<=m AND 0<y

POJ 1745 Divisibility【DP】

题意:给出n,k,n个数,在这n个数之间任意放置+,-号,称得到的等式的值能够整除k则为可划分的,否则为不可划分的. 自己想的是枚举,将所有得到的等式的和算出来,再判断它是否能够整除k,可是有10000个数-_- 5555---还是看的题解-- 话说这样的状态好奇妙啊啊啊--- 用dp[i][j]表示等式中有i个数的时候余数为j是否成立,成立的话dp[i][j]的值为1,否则为0 然后就是递推的过程, 如果dp[i-1][j]为1,那么dp[i][(j-a[i])%k]=1,dp[i][(j+a

poj 1745 Divisibility (dp)

//给你n个数,两个数之间可以+.-两个运算 得出的所有结果只要有能被k正除,输出Divisible,否则Not divisible # include <iostream> # include <stdio.h> # include <algorithm> # include <string.h> using namespace std; int dp[10010][110];//第一维代表几个数相加,第二位为几个数相加%k后的余数 int b[10010