题目大意:n个小于等于m的数,现在你需要在[1,m]中选择若干个数,使得选出的数能组成的所有数正好与n个数相同,给出最少要选多少个数。

题目分析:

结论一:选择的若干个数一定在n个数中。

证明:否则的话不满足“正好”。

结论二:若a,b在由n个数组成的集合中,则a+b(a+b<=m)也在由n个数组成的集合中。

证明:通过归纳法可以证明。

那么我们考虑构造生成函数G(x)=∑ki*xi,其中当由n组成的集合中有数i时ki=1,否则为0。接着将多出的数删除即可。

代码:

#include<bits/stdc++.h>
using namespace std;typedef long long ll;const ll mod = 998244353;
const int gg = 3;
const int maxn = 2520210;
int mp[maxn],a[maxn],f[maxn];
int n,m;
int ord[maxn];int fast_pow(int now,int p){if(p == 0) return 1;if(p == 1) return now;int z = fast_pow(now,p/2); z = (1ll*z*z)%mod;if(p & 1){z = (1ll*z*now)%mod;}return z;
}void fft(int *d,int len,int kind){for(int i=0;i<len;i++) if(ord[i] > i) swap(d[i],d[ord[i]]);for(int i=1;i<len;i<<=1){int wn = fast_pow(gg,(mod-1)/(i<<1));if(kind == -1) wn = fast_pow(wn,mod-2);for(int j=0;j<len;j += (i<<1)){int w = 1;for(int k=0;k<i;k++,w=(1ll*w*wn)%mod){ll x = d[j+k],y = (1ll*w*d[j+k+i])%mod;d[j+k] = (x+y)%mod; d[j+k+i] = (x-y+mod)%mod;}}}if(kind == -1){int inv = fast_pow(len,mod-2);for(int i=0;i<len;i++) d[i] = (1ll*d[i]*inv)%mod;}
}void read(){scanf("%d%d",&n,&m);for(int i=1;i<=n;i++) scanf("%d",&a[i]),mp[a[i]] = f[a[i]] = 1;
}void work(){int bit = 0,len = 1;while(len < m*2) bit++,len<<=1;for(int i=1;i<len;i++) ord[i] = (ord[i>>1]>>1) + ((i&1)<<bit-1);fft(f,len,1);for(int i=0;i<len;i++) f[i] = (1ll*f[i]*f[i])%mod;fft(f,len,-1);int ans = 0;for(int i=0;i<=m;i++){if(f[i]&&mp[i]) {mp[i] = 0;continue;}if(f[i]){puts("NO");return;}if(mp[i]) ans++;}puts("YES");printf("%d\n",ans);for(int i=0;i<=m;i++) if(mp[i]) printf("%d ",i);
}int main(){read();work();return 0;
}

转载于:https://www.cnblogs.com/1-1-1-1/p/8572193.html

codeforces 286E Ladies' Shop相关推荐

  1. CodeForces 286E

    fft #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> u ...

  2. Codeforces Round #325 (Div. 2) B. Laurenty and Shop 前缀和

    B. Laurenty and Shop Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/586/p ...

  3. CodeForces 632E Thief in a Shop(FFT)

    题意:n种物品每种物品有无限个,每个物品有一个价格,现在问选取k个的所以可能总价. 思路:FFT.如果只选择两件商品的话,很容易想到FFT,对于其他的k,只要类似快速幂来求即可,这样做时间复杂度为O( ...

  4. CodeForces - 632E Thief in a Shop 完全背包

    632E:http://codeforces.com/problemset/problem/632/E 参考:https://blog.csdn.net/qq_21057881/article/det ...

  5. Codeforces Round #325 (Div. 2) Laurenty and Shop 模拟

    原题链接:http://codeforces.com/contest/586/problem/B 题意: 大概就是给你一个两行的路,让你寻找一个来回的最短路,并且不能走重复的路. 题解: 就枚举上下选 ...

  6. Codeforces632E Thief in a Shop(NTT + 快速幂)

    题目 Source http://codeforces.com/contest/632/problem/E Description A thief made his way to a shop. As ...

  7. Educational Codeforces Round 12 B. Shopping 暴力

    B. Shopping 题目连接: http://www.codeforces.com/contest/665/problem/B Description Ayush is a cashier at ...

  8. Codeforces Round #570 (Div. 3)B. Equalize Prices

    原题出处:http://codeforces.com/contest/1183/problem/B There are nn products in the shop. The price of th ...

  9. Codeforces Round #644 (Div. 3) D.Buying Shovels

    Codeforces Round #644 (Div. 3) D.Buying Shovels 题目链接 Polycarp wants to buy exactly n shovels. The sh ...

  10. acm CodeForces 546A

    题目来自CodeForces 546A Description A soldier wants to buy w bananas in the shop. He has to pay k dollar ...

最新文章

  1. chromedp网络监听_动态爬虫三:监听网络事件 + 监听js事件
  2. android重启应用
  3. pytorch拼接函数:torch.stack()和torch.cat()--详解及例子
  4. [Swust OJ 247]--皇帝的新衣(组合数+Lucas定理)
  5. 第三次学JAVA再学不好就吃翔(part50)--String类练习
  6. Java工具方法——属性拷贝方法:BeanUtils.copyProperties(Object, Object)
  7. Lisp的本质(The Nature of Lisp)
  8. 一个自己主动依据xcode中的objective-c代码生成类关系图的神器
  9. 背景减法——自组织算法
  10. idea增强for循环
  11. Screen Saver Exporter for Mac(屏幕保护程序导出程序)
  12. 禁用某个程序,试试镜像劫持吧!
  13. 好佳居软装十大品牌 软装拥有与众不同的体验
  14. jsonp跨域原理及使用
  15. 九章算术 八:《方程》
  16. win10+ubuntu18.04lts:在已安装win10环境中利用EasyBCD引导安装Ubuntu
  17. 常见安全算法(RSA、AES、MD5等)原理及实现和win10下OpenSSL库的使用
  18. 怎么计算机删磁盘下游戏,永远留在硬盘里!PC玩家舍不得删除的经典单机游戏(一)...
  19. SWUST oj 254: 翻煎饼
  20. 爬行者LARM---Lucene

热门文章

  1. python Web开发你要理解的WSGI uwsgi详解
  2. AndroidStudio提高编译速度的建议
  3. 私人影院音响效果如何有效提升?
  4. css学习----边框属性
  5. 让块元素在同一行显示的方法: float 和inline-block
  6. TypeError: to_categorical() got an unexpected keyword argument 'nb_classes'
  7. HTML data-* 自定义属性
  8. 鸟哥的Linux私房菜第零章
  9. 浪潮之巅--蓝色巨人读后感
  10. LA 4794 状态DP+子集枚举