Codeforces Gym 101190 NEERC 16 .L List of Primes(递归)
Input
Output
Sample Input
1 35
Sample Output
[2], [3], [2, 3], [5], [2, 5], [7],
题意:把素数集合按照和的大小排序,和相同的按照字典序排序,现在把这个排序后的序列看成一个字符串,问某个区间的字符串是什么。
思路:注意到素数不会太多,只有300来个,素数的和也不会太大,不会超过3000,所以我们递归去找即可。反正就是像数位DP那样搞就行了。。。
用记忆化递推缩小范围,然后用回溯暴力出ans。
#include<bits/stdc++.h>
#define ll long long
#define P pair<ll,ll>
using namespace std;
const int maxn=1e6;
int isprime[maxn],p[maxn];
vector<int>primes;
vector<int>vec;
map<P,P>M;
ll a,b,cur,prefix_len;
int getL(int x){
int res=;
while(x>){ res++; x/=;}
return res+;
}
inline P get(P p1,P p2,ll len){
return P(p1.first+p2.first,p1.second+p2.second+len*p1.first);
}
P calc(int x,int sum)
{
P p(x,sum);
if(sum<) return P(,);
if(M.count(p)) return M[p];
if(sum==) return M[p]=P(,); //ct,len
if(primes[x]>sum) return P(,);
return M[p]=get(calc(x+,sum-primes[x]),calc(x+,sum),getL(primes[x]));
}
void ptchar(char c){
cur++; if(cur>=a&&cur<=b) putchar(c);
}
void print(int x){
vector<int>v;
while(x>){ v.push_back(x%); x/=;}
for(int i=v.size()-;i>=;i--) ptchar(char(v[i]+''));
}
void print(int x,int sum)
{
if(sum<||cur>=b) return ;
if(sum==){
ptchar('[');
for(int i=;i<vec.size();i++){
print(vec[i]);
if(i==vec.size()-) ptchar(']');
ptchar(',');
ptchar(' ');
} return ;
}
if(primes[x]>sum) return ;
vec.push_back(primes[x]);
prefix_len+=getL(primes[x]);
ll len=prefix_len*calc(x+,sum-primes[x]).first+calc(x+,sum-primes[x]).second;
if(len+cur>=a) print(x+,sum-primes[x]);
else cur+=len; vec.pop_back();
prefix_len-=getL(primes[x]);
len=prefix_len*calc(x+,sum).first+calc(x+,sum).second;
if(len+cur>=a) print(x+,sum);
else cur+=len;
}
int main()
{
freopen("list.in","r",stdin);
freopen("list.out","w",stdout);
isprime[]=false;
fill(isprime,isprime+maxn,true);
for(int i=;i<maxn;i++){
if(isprime[i]){
primes.push_back(i);
for(int j=i+i;j<maxn;j+=i) isprime[j]=false;
}
}
scanf("%I64d%I64d",&a,&b);
for(int i=;i<&&cur<b;i++){ //试探过,i最大到2096,这个范围的素数也就300来个,所以记忆化
ll len=calc(,i).second;
if(cur+len>=a) print(,i);
else cur+=len;
}
puts("");
return ;
}
Codeforces Gym 101190 NEERC 16 .L List of Primes(递归)的更多相关文章
- Codeforces Gym 101190 NEERC 16 G. Game on Graph(博弈+拓扑)
Gennady and Georgiy are playing interesting game on a directed graph. The graph has n vertices and m ...
- Codeforces Gym 101190 NEERC 16 .D Delight for a Cat (上下界的费用流)
ls是一个特别堕落的小朋友,对于n个连续的小时,他将要么睡觉要么打隔膜,一个小时内他不能既睡觉也打隔膜 ,因此一个小时内他只能选择睡觉或者打隔膜,当然他也必须选择睡觉或打隔膜,对于每一个小时,他选择睡 ...
- 【Codeforces Gym 100725K】Key Insertion
Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...
- Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】
2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...
- codeforces gym 100553I
codeforces gym 100553I solution 令a[i]表示位置i的船的编号 研究可以发现,应是从中间开始,往两边跳.... 于是就是一个点往两边的最长下降子序列之和减一 魔改树状数 ...
- Codeforces Gym 101252D&&floyd判圈算法学习笔记
一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...
- Codeforces Gym 101190M Mole Tunnels - 费用流
题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...
- Codeforces Gym 101623A - 动态规划
题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...
- CodeForces Gym 100213F Counterfeit Money
CodeForces Gym题目页面传送门 有\(1\)个\(n1\times m1\)的字符矩阵\(a\)和\(1\)个\(n2\times m2\)的字符矩阵\(b\),求\(a,b\)的最大公共 ...
随机推荐
- 广告banner:手动滑动切换,自动切换,点击跳转,异步加载网络图片
效果图: 该banner功能有自动切换图片,点击图片可以自定义事件,手动滑动切换,异步加载图片 代码说话: 布局文件: <!-- 广告位 --> <FrameLayout andro ...
- iOS陆哥开发笔记(七) (AVFoundation简单介绍)
在AVFoundation框架中AVAudioRecorder类专门处理录音操作,支持多种音频格式. 以下是经常使用的属性和方法: 属性 说明 @property(readonly, getter=i ...
- 加入 centos 右键 terminal
centos6.2以上默认右键都没有terminal,现加入方法 例如以下 1> yum -y install nautilus-open-terminal 2> shutdown -r ...
- python学习(六)元组学习
元组就是列表的一种,不过元组具有不可变性,而且是用圆括号访问的. 索引(下表索引或者键索引都是用的中括号) #!/usr/bin/python # 这节来学习元组, tuple, 基本上就像一个不可以 ...
- 深入Asyncio(九)异步生成器
Async Generators:yield inside async def functions 如果在async def中使用yield会发生什么,答案就是生成一个异步生成器函数,如果有生成器.协 ...
- PowerBuilder -- Len(), LenA() 与 String, Blob
使用的是Powerbuilder12.5与Powerbuild9 不太一样 函数 String Blob Len() 返回字符数 返回字符数对应的字节数 LenA() 返回字节数 返回字符数对应的字节 ...
- python 基础 1.5 python 数据类型(一)--整型 浮点型 布尔型及字符串和常用方法
一.python 数据类型:数值,字符串,列表,元组,字典.以下操作是在linux 下 ipython中进行 1.数值 1>123 与 “123”的区别 答:123为数值,“123”在pyt ...
- 九度OJ 1059:abc (基础题)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3642 解决:2869 题目描述: 设a.b.c均是0到9之间的数字,abc.bcc是两个三位数,且有:abc+bcc=532.求满足条件的 ...
- Mac下终端常用命令
一.删除文件 1 打开终端应用程序 2 输入命令:sudo (空格) rm (空格)-r (空格)-f (空格)(注意-f后面还有空格),还要注意,全部小写. 3 把你要删的文件或者文件夹用mouse ...
- 基于Flume的美团日志收集系统 架构和设计 改进和优化
3种解决办法 https://tech.meituan.com/mt-log-system-arch.html 基于Flume的美团日志收集系统(一)架构和设计 - https://tech.meit ...