HDU5812 Distance 构造,预处理
分析:怎么看都是超时,但是可以先筛一遍1e6以内的每个数的最小素数
算出每个数由多少个素数组成,然后应用,c[1e6][20]
就是题解的那一套,参照题解,比赛的时候没有想到好的办法筛一个数的因子,醉了
然后赛后发现,预处理因子肯定超时,虽然是O(nlogn)的,但是n是1e6啊,常数太大
而且单组操作只有5e4,所以暴力sqrt(x)即可
#include <iostream>
#include <vector>
#include <queue>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e6+;
int c[N][],tot,prime[],mn[N],cnt[N],q,kase;
bool check[N];
char op[];
void getprime()
{
for(int i=; i<=N-; ++i)
{
if(!check[i])mn[i]=prime[++tot]=i;
for(int j=; j<=tot; ++j)
{
if(i*prime[j]>N-)break;
check[i*prime[j]]=true;
mn[i*prime[j]]=prime[j];
if(i%prime[j]==)break;
}
}
}
void getcnt()
{
for(int i=; i<=N-; ++i)
{
int tmp=i;
while(tmp!=)++cnt[i],tmp/=mn[tmp];
}
}
int main()
{
getprime();
getcnt();
memset(mn,,sizeof(mn));
while(~scanf("%d",&q),q)
{
printf("Case #%d:\n",++kase);
memset(c,,sizeof(c));
int ttt=;
for(int i=; i<q; ++i)
{
int x;
scanf("%s%d",op,&x);
if(op[]=='I')
{
if(mn[x]==kase)continue;
mn[x]=kase;++ttt;
for(int j=; j*j<=x; ++j)
{
if(x%j)continue;
++c[j][cnt[x/j]];
if(x/j!=j)++c[x/j][cnt[j]];
}
}
else if(op[]=='D')
{
if(mn[x]!=kase)continue;
mn[x]=;--ttt;
for(int j=; j*j<=x; ++j)
{
if(x%j)continue;
--c[j][cnt[x/j]];
if(x/j!=j)--c[x/j][cnt[j]];
}
}
else
{
if(ttt==){printf("-1\n");continue;}
int ans=;
for(int j=; j*j<=x; ++j)
{
if(x%j)continue;
for(int k=; k<=; ++k)
{
if(c[j][k])
{
ans=min(ans,k+cnt[x/j]);
break;
}
}
if(x/j!=j)
{
for(int k=; k<=; ++k)
{
if(c[x/j][k])
{
ans=min(ans,k+cnt[j]);
break;
}
}
}
}
if(ans==)ans=-;
printf("%d\n",ans);
}
}
}
return ;
}
HDU5812 Distance 构造,预处理的更多相关文章
- HDU5812 Distance(枚举 + 分解因子)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5812 Description In number theory, a prime is a ...
- LeetCode---String
Count and Say 思路:递归求出n - 1时的字符串,然后双指针算出每个字符的次数,拼接在结果后面 public String countAndSay(int n) { if(n == 1) ...
- java,大数据批量插入、更新
public void exec(Connection conn){ try { conn.setAutoCommit(false); Long beginTime = System.currentT ...
- CodeForces 631C Print Check
排序+构造+预处理 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm ...
- 用Java向数据库中插入大量数据时的优化
使用jdbc向数据库插入100000条记录,分别使用statement,PreparedStatement,及PreparedStatement+批处理3种方式进行测试: public void ex ...
- 【霍夫曼树】poj 1339 poker card game (数组排序+辅助队列的方法,预处理O(nlogn),构造霍夫曼树O(n))
poj.org/problem?id=1339 #include<iostream> #include<cstdio> #include<string> #incl ...
- Distance Dependent Infinite Latent Feature Model 阅读笔记1
阅读文献:Distance Dependent Infinite Latent Feature Model 作者:Samuel J.Gershman ,Peter I.Frazier ,and Dav ...
- Codeforces CF#628 Education 8 C. Bear and String Distance
C. Bear and String Distance time limit per test 1 second memory limit per test 256 megabytes input s ...
- CF #296 (Div. 1) B. Clique Problem 贪心(构造)
B. Clique Problem time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
随机推荐
- Github 学习
1.git$cd ~/hello-world.$git add . //这样可以自动判断新加了哪些文件,或者手动加入文件名字$git commit //提交到本地仓库,不加参数会提示,注意:^=Ctr ...
- Linux命令-tar
tar命令用于对文件打包压缩或解压 格式:tar [选项 -C 指定解压到的目录] [文件] 打包并压缩文件: tar -zcvf 压缩包名.tar.gz 文件名 解压并展开压缩包 tar -zxvf ...
- mysql 生成批量存储过程
CREATE PROCEDURE `BatchInsert`(IN init INT, IN loop_time INT)BEGIN DECLARE Var INT; DECLARE ID INT; ...
- linux read和write函数
原文出处:http://blog.chinaunix.net/space.php?uid=20558494&do=blog&id=2803003read函数是Linux下不带缓存的文件 ...
- ISE MAP报错: Unsupported programming for BSCAN block and JTAG_CHAIN attribute value 1的解决方法
2014-04-16 17:35:30 ISE MAP报错: Unsupported programming for BSCAN block and JTAG_CHAIN attribute valu ...
- sql server 2008r2 清除数据库日志
USE [master] GO ALTER DATABASE data SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE data SET REC ...
- 使用public key来做SSH authentication
public key authentication(公钥认证)是对通过敲用户名.密码方式登录服务器的一种替代办法.这种方法更加安全更具有适应性,但是更难以配置. 传统的密码认证方式中,你通过证明你你知 ...
- file类型允许的文件格式设置问题,“选择文件”打开缓慢
1,file类型的input对于打开的选择框的属性是由以下两个属性控制的: ①multiple="multiple" :一次可以选择多个文件 ②accept="image ...
- HDU 3127 WHUgirls【二维完全背包】
题意:给出一个长为a,宽为b的布,再给出n个围巾的规格(长x,宽y,价值c),问怎样裁剪能够得到最大的价值. ----第一次做的时候不会---然后放到今天做--发现还是不会---于是又--看题解了-- ...
- Phpstorm Xdebug Web程序调试
平时调试php程序的时候,可以通过在代码中添加var_dump等函数来实现简单的断点调试. 下面介绍另一种方法,通过Phpstorm和Xdebug来进行调试. 1.下载Xdebug 这个是官网下载地址 ...