C - NP-Hard Problem

Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

Pari wants to buy an expensive chocolate from Arya. She has n coins, the value of the i-th coin is ci. The price of the chocolate is k, so Pari will take a subset of her coins with sum equal to k and give it to Arya.

Looking at her coins, a question came to her mind: after giving the coins to Arya, what values does Arya can make with them? She is jealous and she doesn't want Arya to make a lot of values. So she wants to know all the values x, such that Arya will be able to make x using some subset of coins with the sum k.

Formally, Pari wants to know the values x such that there exists a subset of coins with the sum k such that some subset of this subset has the sum x, i.e. there is exists some way to pay for the chocolate, such that Arya will be able to make the sum x using these coins.

Input

The first line contains two integers n and k (1  ≤  n, k  ≤  500) — the number of coins and the price of the chocolate, respectively.

Next line will contain n integers c1, c2, ..., cn (1 ≤ ci ≤ 500) — the values of Pari's coins.

It's guaranteed that one can make value k using these coins.

Output

First line of the output must contain a single integer q— the number of suitable values x. Then print q integers in ascending order — the values that Arya can make for some subset of coins of Pari that pays for the chocolate.

Sample Input

Input
6 18
5 6 1 10 12 2
Output
16
0 1 2 3 5 6 7 8 10 11 12 13 15 16 17 18
Input
3 50
25 25 50
Output
3
0 25 50

Sample Output

 

Hint

 

Description

Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex cover problem very interesting.

Suppose the graph G is given. Subset A of its vertices is called a vertex cover of this graph, if for each edge uv there is at least one endpoint of it in this set, i.e. or (or both).

Pari and Arya have won a great undirected graph as an award in a team contest. Now they have to split it in two parts, but both of them want their parts of the graph to be a vertex cover.

They have agreed to give you their graph and you need to find two disjoint subsets of its vertices A and B, such that both A and B are vertex cover or claim it's impossible. Each vertex should be given to no more than one of the friends (or you can even keep it for yourself).

Input

The first line of the input contains two integers n and m (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000) — the number of vertices and the number of edges in the prize graph, respectively.

Each of the next m lines contains a pair of integers ui and vi (1  ≤  ui,  vi  ≤  n), denoting an undirected edge between ui and vi. It's guaranteed the graph won't contain any self-loops or multiple edges.

Output

If it's impossible to split the graph between Pari and Arya as they expect, print "-1" (without quotes).

If there are two disjoint sets of vertices, such that both sets are vertex cover, print their descriptions. Each description must contain two lines. The first line contains a single integer k denoting the number of vertices in that vertex cover, and the second line contains k integers — the indices of vertices. Note that because of m ≥ 1, vertex cover cannot be empty.

Sample Input

Input
4 2
1 2
2 3
Output
1
2
2
1 3
Input
3 3
1 2
2 3
1 3
Output
-1

Sample Output

 

Hint

In the first sample, you can give the vertex number 2 to Arya and vertices numbered 1 and 3 to Pari and keep vertex number 4 for yourself (or give it someone, if you wish).

In the second sample, there is no way to satisfy both Pari and Arya.

题意:告诉你xmod(c1,c2,c3...),问你是否能求出xmodk

思路:求给出的c1,c2,c3,,,中质因子是否能组成k的倍数。

代码:

#include <iostream>
#include <cstdio>
using namespace std;
inline int gcd(int a,int b)
{
if(a%b==0)
return b;
return gcd(b,a%b);
}
inline int lcm(int a,int b)
{
return a/gcd(a,b)*b;
}
int main()
{ int n,k;
while(cin>>n>>k)
{ int l=1;
int a;
for(int i=0;i<n;i++)
{ scanf("%d",&a);
int g=gcd(a,k);
l=lcm(l,g);
}
if(l==k)
printf("Yes\n");
else
printf("No\n"); } }

C - NP-Hard Problem的更多相关文章

  1. 集合覆盖 顶点覆盖: set cover和vertex cover

    这里将讲解一下npc问题中set cover和vertex cover分别是什么. set cover: 问题定义: 实例:现在有一个集合A,其中包含了m个元素(注意,集合是无序的,并且包含的元素也是 ...

  2. Faster RCNN 爬坑记录

    训练 在博客http://blog.csdn.net/Suii_v5/article/details/73776299中介绍了基本的错误类型.我只是做一些自己问题的补充 在error6中,调整nump ...

  3. 证明与计算(1): Decision Problem, Formal Language L, P and NP

    0x01 从判定问题到形式语言 这篇讲知识证明的wiki([1]): https://en.wikipedia.org/wiki/Proof_of_knowledge 里面有一句话: Let x be ...

  4. NP问题/NP完全问题(NP-complete problem)如何判断是否是NP完全问题

    在算法复杂度分析的过程中,人们常常用特定的函数来描述目标算法,随着变量n的增长,时间或者空间消耗的增长曲线,近而进一步分析算法的可行性(有效性). 引入了Big-O,Big-Ω,来描述目标算法的上限. ...

  5. 转载 什么是P问题、NP问题和NPC问题

    原文地址http://www.matrix67.com/blog/archives/105 这或许是众多OIer最大的误区之一.    你会经常看到网上出现“这怎么做,这不是NP问题吗”.“这个只有搜 ...

  6. HDU1760 A New Tetris Game NP态

    A New Tetris Game Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  7. Gambler's Ruin Problem and 3 Solutions

    In my stochastic processes class, Prof Mike Steele assigned a homework problem to calculate the ruin ...

  8. (数学)P、NP、NPC、NP hard问题

    概念定义: P问题:能在多项式时间内解决的问题: NP问题:(Nondeterministic Polynomial time Problem)不能在多项式时间内解决或不确定能不能在多项式时间内解决, ...

  9. P,NP,NP_hard,NP_complete问题定义

    背景:在看李航的<统计学习方法时>提到了NP完全问题,于是摆之. 问题解答:以下是让我豁然开朗的解答的摘抄: 最简单的解释:P:算起来很快的问题NP:算起来不一定快,但对于任何答案我们都可 ...

  10. P和NP问题

    1. 通俗详细地讲解什么是P和NP问题 http://blog.sciencenet.cn/blog-327757-531546.html   NP----非定常多项式(英语:non-determin ...

随机推荐

  1. Android 两个activity生命周期的关系

    Acitivity的生命周期想必大家都清楚,但是两个activity之间其实不是独立各自进行的. 从第一个activity1启动另外一个activity2时,会先调用本activity1的onPaus ...

  2. [nRF51822] 8、基础实验代码解析大全 · 实验11 - PPI

    前一篇分析了前十个基础实验的代码,从这里开始分析后十个~ 一.PPI原理: PPI(Programmable Peripheral Interconnect),中文翻译为可编程外设互连. 在nRF51 ...

  3. 文件上传之Apache commons fileupload使用

    后台servlet代码:         File file1 = null,file2=null;         String description1 = null,description2 = ...

  4. sql复习第五次

    1.在数据库范围内,关系的每一个属性值是不可分解的 关系中不允许出现重复元组 由于关系是一个集合,因此不考虑元组的顺序 2.笛卡儿积是两个关系的所有元组组合而成的,而等值联接是由笛卡儿积和选择运算组合 ...

  5. 《Qt Quick 4小时入门》学习笔记3

    http://edu.csdn.net/course/detail/1042/14807?auto_start=1 Qt Quick 4小时入门 第八章:Qt Quick中的锚(anchors)布局 ...

  6. python中configparser模块

    python中的configparse模块的使用 主要用来解析一些常用的配置,比如数据配置等. 例如:有一个dbconfig.ini的文件 [section_db1] db = test_db1 ho ...

  7. 无法启动WP Emulator

    记得以前Vware不能运行设置的东西了吗?http://www.cnblogs.com/dunitian/p/4480750.html 如果不清楚可以参考上面的链接 重启的时候选择第二项 重新打开就o ...

  8. 传智播客--数据绑定--INotifyPropertyChanged(小白内容)

    INotifyPropertyChanged一般在数据绑定的时候使用. InotifyPropertyChanged是.net内置的接口,数据绑定时会检测DataContext是否实现了Inotify ...

  9. 深入理解javascript对象系列第二篇——属性操作

    × 目录 [1]查询 [2]设置 [3]删除[4]继承 前面的话 对于对象来说,属性操作是绕不开的话题.类似于“增删改查”的基本操作,属性操作分为属性查询.属性设置.属性删除,还包括属性继承.本文是对 ...

  10. Power BI官方视频(1) Power BI Desktop 7月份更新功能概述

    2016年7月,Power BI Desktop进行了一些功能更新,提高整体的用户体验.同时也有一些新的和令人兴奋的功能.看看大概介绍,更新功能要点: 本文原文地址:Power BI官方视频(1) P ...