[杂题] Codeforces Gym 101190 NEERC 16 L. List of Primes
来源:互联网 发布:html javascript集群 编辑:程序博客网 时间:2024/05/22 14:07
关键是一个函数Solve(int x,int *pre,int sum,int L)
表示已经考虑完了前x个质数 得到前缀pre 剩下需要的和为sum 下标从L开始
然后需要预处理下不用前x个质数 组成sum 的方案数和总长
然后超出范围的时候不处理
就能过了
#include<cstdio>#include<cstdlib>#include<algorithm>using namespace std;typedef long long ll;const int maxn=7700;int pri[maxn+5],num;int vst[maxn+5];int m=0,n;ll f[1005][10005],g[1005][10005];inline int len(int x){ int ret=0; while (x) ret++,x=x/10; return ret+2;}inline void Pre(){ for (int i=2;i<=maxn;i++){ if (!vst[i]) pri[++num]=i; for (int j=1;j<=num && pri[j]*i<=maxn;j++){ vst[i*pri[j]]=1; if (i%pri[j]==0) break; } } n=num; m=0; for (int i=1;i<=60;i++) m+=pri[i]; f[n+1][0]=0; g[n+1][0]=1; for (int i=n;i;i--) for (int j=0;j<=m;j++){ f[i][j]=f[i+1][j],g[i][j]=g[i+1][j]; if (j>=pri[i]){ f[i][j]+=f[i+1][j-pri[i]]+g[i+1][j-pri[i]]*len(pri[i]); g[i][j]+=g[i+1][j-pri[i]]; } }}int first=0; ll start=0;char cur[100005]; int pnt=0;inline void push(int x){ static char tmp[15],len; len=0; while (x) tmp[++len]=x%10+'0',x/=10; for (int i=len;i;i--) cur[++pnt]=tmp[i]; cur[++pnt]=','; cur[++pnt]=' ';}ll L,R;char ans[1000005]; int p;inline void Solve(int x,int sum,ll sta){ if (sta>R) return; if (sta+f[x+1][sum]+g[x+1][sum]*(2+pnt)<L) return; if (!sum){ if (!first) first=1,start=sta+1; ans[++p]='['; for (int i=1;i<=pnt-2;i++) ans[++p]=cur[i]; ans[++p]=']'; ans[++p]=','; ans[++p]=' '; if (start && start+p-1>=R){ for (ll i=L;i<=R;i++) putchar(ans[i-start+1]); exit(0); } return; } if (x==n) return; if (f[x+1][sum]==0 || sum<pri[x+1]) return; int tmp=pnt; push(pri[x+1]); Solve(x+1,sum-pri[x+1],sta); sta+=f[x+2][sum-pri[x+1]]+g[x+2][sum-pri[x+1]]*(2+pnt); pnt=tmp; Solve(x+1,sum,sta);}int main(){ freopen("list.in","r",stdin); freopen("list.out","w",stdout); scanf("%I64d%I64d",&L,&R); Pre(); ll t=0; for (int i=2;;i++){ Solve(0,i,t); t+=f[1][i]+g[1][i]*2; } return 0;}
0 0
- [杂题] Codeforces Gym 101190 NEERC 16 L. List of Primes
- [物理 杂题] Codeforces Gym 101190 NEERC 16 J. Jenga Boom
- [杂题] Codeforces Gym 101190 NEERC 16 K. Kids Designing Kids
- [博弈论] Codeforces Gym 101190 NEERC 16 G. Game on Graph
- [构造] Codeforces Gym 101190 NEERC 16 C. Cactus Construction
- [扫描线 杂题] Codeforces Gym 101190 NEERC 16 E. Expect to Wait
- [杂题 交互] Codeforces Gym 101190 NEERC 16 I. Indiana Jones and the Uniform Cave
- Codeforces Gym 101190 (NEERC 2016) A. Abbreviation
- [树形DP 费用流手动增广] Codeforces Gym 101190 NEERC 16 M. Mole Tunnels
- [Trie树建图 2-SAT] Codeforces Gym 101190 NEERC 16 B. Binary Code
- [费用流建模] Codeforces Gym 101190 NEERC 16 D. Delight for a Cat
- Codeforces Gym 101190 (NEERC 2016) F. Foreign Postcards (dp + 期望)
- Codeforces Gym 101190 (NEERC 2016) J. Jenga Boom
- Codeforces Gym 101190 NEERC 2016 B. Binary Code
- [交互 杂题] Codeforces Gym 100307 NEERC 13 I. Interactive Interception
- Codeforces Gym 101190 (NEERC 2016) E. Expect to Wait (扫描线)
- Codeforces Gym 101190 (NEERC 2016) H. Hard Refactoring (模拟 + 树状数组)
- codeforces gym 2016-2017 NEERC, Moscow Subregional K. Knights of the Old Republic 最小生成树+dp
- the diary of script in linux
- js校验电话号码、邮箱、金额、身份证号、密码长度和复杂程度等等的验证
- warning :uninstalling will remove the application data!
- apache301重定向(不带www转向带www域名)
- MySQL主从复制从库状态监测
- [杂题] Codeforces Gym 101190 NEERC 16 L. List of Primes
- Bootboxjs是一个简单的js库,简单快捷帮你制作一个Bootstrap的弹出框效果。
- POJ
- hbase伪分布式安装——腾讯云
- 自定义键盘
- 博文导航
- c++用标准输入输出流测试数据
- vector,list,map,set
- ScheduleThreadPoolExecutor的工作原理与使用示例