Codeforce 286E
FFT 好题    2017-01-03 20:21:37    164    0    0
jszyxw   FFT 好题

Ladies' Shop

Description

A ladies' shop has recently opened in the city of Ultima Thule. To get ready for the opening, the shop bought n bags. Each bag is characterised by the total weight ai of the items you can put there. The weird thing is, you cannot use these bags to put a set of items with the total weight strictly less than ai. However the weights of the items that will be sold in the shop haven't yet been defined. That's what you should determine right now.

Your task is to find the set of the items' weights p1, p2, ..., pk (1 ≤ p1 < p2 < ... < pk), such that:

  1. Any bag will be used. That is, for any i (1 ≤ i ≤ n) there will be such set of items that their total weight will equal ai. We assume that there is the infinite number of items of any weight. You can put multiple items of the same weight in one bag.
  2. For any set of items that have total weight less than or equal to m, there is a bag into which you can put this set. Similarly, a set of items can contain multiple items of the same weight.
  3. Of all sets of the items' weights that satisfy points 1 and 2, find the set with the minimum number of weights. In other words, value kshould be as small as possible.

Find and print the required set.

Input

The first line contains space-separated integers n and m (1 ≤ n, m ≤ 106). The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ a1 < a2 < ... < an ≤ m) — the bags' weight limits.

Output

In the first line print "NO" (without the quotes) if there isn't set pi, that would meet the conditions.

Otherwise, in the first line print "YES" (without the quotes), in the second line print an integer k (showing how many numbers are in the suitable set with the minimum number of weights), in the third line print k space-separated integers p1, p2, ..., pk(1 ≤ p1 < p2 < ... < pk). If there are multiple solutions, print any of them.

 

Solution

如果有解,只存在两种情况

    1.a[i]不能被任意的a[j]所组成

    2.a[i]能被两个a[j]组成

卷积一下,有

若[xi]A=1且[xi]A2=0,则i一定在答案中
若[xi]A=0且[xi]A2=1,无解

Source code

#include <bits/stdc++.h>

#ifdef __linux__
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif

typedef long long ll;
typedef std::pair<int, int> pii;

std::string Name = __FILE__;
std::string iput = Name.substr(0, Name.length() - 4) + ".in";
std::string oput = Name.substr(0, Name.length() - 4) + ".out";

template <class T> inline bool chkmin(T &x, T y) { return x > y ? x = y, true : false; }
template <class T> inline bool chkmax(T &x, T y) { return x < y ? x = y, true : false; }

template <class T> inline T &read(T &x)
{
    static int f;
    static char c; 
    for (f = 1; !isdigit(c = getchar()); ) {
        if (c == '-')
            f = -1;
    }
    for (x = 0; isdigit(c); c = getchar()) {
        x = x * 10 + c - 48;
    }
    return x *= f;
}
 
template <class T> inline void write(T x, const char p = '\n')
{
    static int top;
    static int s[30];
    if (x < 0) {
        x = -x;
        putchar('-');
    }
    do s[++ top] = x % 10 + 48;
    while (x /= 10);
    while (top)
        putchar(s[top --]);
    putchar(p);
}
 
const int maxn = 1 << 21;
const double pi = acos(-1.0);

struct Complex
{
    double x, y;

    Complex(double x = 0, double y = 0):x(x), y(y) { }

    Complex operator + (const Complex &a) const { return Complex(x + a.x, y + a.y); }
    Complex operator - (const Complex &a) const { return Complex(x - a.x, y - a.y); }
    Complex operator * (const Complex &a) const { return Complex(x * a.x - y * a.y, y * a.x + x * a.y); }
};

int N, Log;
int rev[maxn];

void DFT(Complex a[], int flag)
{
    for (int i = 0; i < N; ++ i)
        if (i < rev[i])
            std::swap(a[i], a[rev[i]]);

    for (int k = 1; k < N; k <<= 1) {
        Complex one(cos(pi / k), flag * sin(pi / k));

        for (int i = 0; i < N; i += 2 * k) {
            Complex now(1.0, 0.0);

            for (int j = 0; j < k; ++ j) {
                Complex x = a[i + j];
                Complex y = a[i + k + j] * now;
                a[i + j] = x + y;
                a[i + k + j] = x - y;
                now = now * one;
            }
        }
    }
    
    if (flag != -1) return;

    for (int i = 0; i < N; ++ i) {
        a[i].x /= N;
    }
}

bool vis[maxn];
bool vis2[maxn];

Complex a[maxn];

int m;

void FFT()
{
    for (N = 1; N < m * 2; N <<= 1, ++ Log) ;

    for (int i = 0; i < N; ++ i) {
        rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (Log - 1));
    }

    DFT(a, 1);
    for (int i = 0; i < N; ++ i) {
        a[i] = a[i] * a[i];
    }
    DFT(a, -1);

    for (int i = 0; i < N; ++ i) {
        if (round(a[i].x) > 0)
            vis2[i] = true;
    }
}
 
int n;

void exec()
{
    read(n);
    read(m);
    for (int i = 1, x; i <= n; ++ i) {
        read(x);
        a[x].x = 1.0;
        vis[x] = true;
    }

    FFT();

    static std::queue<int> ans;
    for (int i = 1; i <= m; ++ i) {
        if (vis[i] && !vis2[i]) ans.push(i);
        if (!vis[i] && vis2[i]) puts("NO"), exit(0);
    }

    puts("YES");
    write(ans.size());
    while (!ans.empty()) {
        write(ans.front(), ' ');
        ans.pop();
    }
}

/*{{{*/
int main() 
{
    if (fopen(iput.c_str(), "r") != NULL) {
        freopen(iput.c_str(), "r", stdin);
        freopen(oput.c_str(), "w", stdout);
    }

    exec();

    fclose(stdin);
    fclose(stdout);

    return 0;
}
/*}}}*/


BZOJ3065: 带插入区间K小值
164 人读过
立即登录, 发表评论.
没有帐号? 立即注册
0 条评论
文档导航