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\)的最大公共 ...
随机推荐
- Leetcode Array 16 3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- 介绍JSON
0x00 介绍JSON 介绍JSON :http://www.json.org/json-zh.html Introducing JSON :http://www.json.org/
- MP4文件格式的解析,以及MP4文件的分割算法
http://www.cnblogs.com/haibindev/archive/2011/10/17/2214518.html http://blog.csdn.net/pirateleo/arti ...
- golang 格式化时间成datetime
Golang或者Beego,总需要往数据库里写datetime时间戳. Golang对时间格式支持并不理想. 先看一个例子: package main import ( "fmt" ...
- 11 linux nginx上安装ecshop 案例
一: nginx上安装ecshop 案例 (1)解压到 nginx/html下 浏览器访问:127.0.0.1/ecshop/index.php 出现错误:not funod file 原因:ngin ...
- ubuntu apt 主要命令及参数
1. apt-cache search package 搜索安装包 2. apt-cache search all 搜索所有安装包 3. apt-cache show package 显示安装包信息 ...
- 【Atheros】如何在驱动中禁用ACK
上一篇文章讲了如何禁用载波侦听(CSMA)和退避(BACKOFF)的方法,这一篇介绍如何禁用ACK. 禁用ACK主要分为三部分: 1. 在发送端设置不等待ACK回来就继续发送: 2. 在接收端设置收到 ...
- C - The C Answer (2nd Edition) - Exercise 1-1
/* Run the "hello, world" program on your system. Experiment with leaving out parts of the ...
- ifndef/define/endif 和 #ifdef 、#if 作用和用法
为了能简单的看看某些linux内核源码,复习了一下c语音,今天汇总了一下关于宏定义的相关内容: 一.ifndef/define/endif用法: .h文件,如下: #ifndef XX_H #defi ...
- 【题解】[CJOI2019]Cipher
[题解][CJOI2019]Cipher 题目描述 给定你\(p\)进制数\(s\),\(p \le 9+26\),求对于十进制数\(k\),求\(k^s \equiv ? \mod m\) 数据范围 ...