zoukankan      html  css  js  c++  java
  • C

    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
    ");
            else
            printf("No
    ");
    
        }
    
    }
    
  • 相关阅读:
    candy——动态规划
    HTTP协议(转)
    linked-list-cycle-ii——链表,找出开始循环节点
    linked-list-cycle——链表、判断是否循环链表、快慢指针
    转: utf16编码格式(unicode与utf16联系)
    【转】Nginx模块开发入门
    【转】依赖注入那些事儿
    转: OpenResty最佳实践
    转:linux的源码查看, c++语法 查看网站
    【转】所需即所获:像 IDE 一样使用 vim
  • 原文地址:https://www.cnblogs.com/llsq/p/5893799.html
Copyright © 2011-2022 走看看