分析:怎么看都是超时,但是可以先筛一遍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 构造,预处理的更多相关文章

  1. HDU5812 Distance(枚举 + 分解因子)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5812 Description In number theory, a prime is a ...

  2. LeetCode---String

    Count and Say 思路:递归求出n - 1时的字符串,然后双指针算出每个字符的次数,拼接在结果后面 public String countAndSay(int n) { if(n == 1) ...

  3. java,大数据批量插入、更新

    public void exec(Connection conn){ try { conn.setAutoCommit(false); Long beginTime = System.currentT ...

  4. CodeForces 631C Print Check

    排序+构造+预处理 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm ...

  5. 用Java向数据库中插入大量数据时的优化

    使用jdbc向数据库插入100000条记录,分别使用statement,PreparedStatement,及PreparedStatement+批处理3种方式进行测试: public void ex ...

  6. 【霍夫曼树】poj 1339 poker card game (数组排序+辅助队列的方法,预处理O(nlogn),构造霍夫曼树O(n))

    poj.org/problem?id=1339 #include<iostream> #include<cstdio> #include<string> #incl ...

  7. Distance Dependent Infinite Latent Feature Model 阅读笔记1

    阅读文献:Distance Dependent Infinite Latent Feature Model 作者:Samuel J.Gershman ,Peter I.Frazier ,and Dav ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Centos环境下部署游戏服务器-Eclipse

    一直在想这篇文章该不该写,因为这篇文章更像是教你如何使用一个ide这种文章,毫无价值可言.但思来想去还是应给写.上篇文章主要说了编译原理和过程,这篇文章就是理论联系实际的典范.并且很多工程师一辈子都不 ...

  2. Qt之界面出现、消失动画效果(简单好用)

    在学习Qt的这2.3个月里,对Qt越发感兴趣,从刚开始的盲目.无所适从到现在的学习.研究.熟练.掌握的过程中,我学到了很多东西,也学会了如何通过自学让自己更加成熟.强大起来,如何更有效地提高自己学习. ...

  3. MySql对空间数据库的支持

    地址: MySQL5.1中文在线API:http://doc.mysql.cn/mysql5/refman-5.1-zh.html-chapter/spatial-extensions-in-mysq ...

  4. swift:入门知识之简单值

    1.swift中用let关键字类定义常量,用var关键字来定义变量. 2.swift语句的结尾不需要再带逗号,系统在运行程序时自动会帮你添加上 3.一个变量或常量必须与赋值时拥有相同的类型. 4.如果 ...

  5. USACO Section 3.3: Home on the Range

    到最后发现是DP题 /* ID: yingzho1 LANG: C++ TASK: range */ #include <iostream> #include <fstream> ...

  6. Android之开发杂记(一)

    1.cygwin环境变量设置 可在Cygwin.bat 中设置 set NDK_ROOT=P:/android/android-ndk-r8e 或者在home\Administrator\.bash_ ...

  7. vim快捷键笔记【原创】

    Vim zR  全部展开 zM全部合并 vim  快捷键 shift   +  i      (‘I’)    进行编辑 shift   +  4     (‘$’)    跳到行尾 shift   ...

  8. html5 移动端单页面布局

    序     移动端的web网页使用的是响应式设计,但一般我们看到的网站页面都是跳转刷新得到的,比如通过点击一个menu后进入到另一个页面 今天来说下是移动端的单页面的布局.单页面就是一切操作和布局都是 ...

  9. [NYIST32]组合数(状压,枚举,暴力)

    题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=32 求n个数中挑出r个数字的所有情况,最后倒序输出所有情况. 状压枚举所有情况就是了 ...

  10. 注意map<> 的[]

    其实在之前一篇关于map的基本操作中已经提到过注意[]操作,这里再强调一下. 先看下面的程序: #include<iostream> #include<map> using n ...