分析:怎么看都是超时,但是可以先筛一遍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. linux下python启动第三方程序,并控制关闭

    import subprocess import os import signal p = subprocess.Popen("recordmydesktop -o /home/test/t ...

  2. Redis的List操作

    lpush key value 作用: 把值插入到链接头部 127.0.0.1:6379> lpush character a (integer) 1 127.0.0.1:6379> rp ...

  3. Java:抽象类abstract和接口Interface

    一.抽象类:abstract 抽象类就是为了继承而存在的,如果你定义了一个抽象类,却不去继承它,那么等于白白创建了这个抽象类,因为你不能用它来做任何事情.对于一个父类,如果它的某个方法在父类中实现出来 ...

  4. USACO Section 3.1: Stamps

    这题一开始用了dfs(注释部分),结果TLE,后来想了DP方法,f[i] = f[j] + f[i-j], j = 1, 2... i/2, 还是TLE,网上搜了别人的代码,发现自己的状态方程有问题, ...

  5. USACO Section 3.1: Agri-Net

    minimal spanning tree的经典题 /* ID: yingzho1 LANG: C++ TASK: agrinet */ #include <iostream> #incl ...

  6. js金额转换大写

    从tenpay找到的一段将金额小写转换成大写的js代码: var DX = function (num) { var strOutput = ""; var strUnit = ' ...

  7. Eclipse 修改debug当前行的颜色

    window --preferences--general--editors--text editors--annotations--debug current instruction pointer

  8. android 分区layout以及虚拟内存布局-小结

    摘要 简述启动过程的内存分配,各个映像的烧写,加载,logo的刷新,文件系统mount. DRAM:外部RAM: ISRAM:内部RAM(128K),(PL会跑在ISRAM里面,去初始化DRAM,lo ...

  9. Simple Factory vs. Factory Method vs. Abstract Factory【简单工厂,工厂方法以及抽象工厂的比较】

    I ran into a question on stackoverflow the other day that sort of shocked me. It was a piece of code ...

  10. Indoor Positioning System & Real time location system

    背景 惨痛的背景,正如我前面提到的,参加了公司的一个训练营.刚进来公司的新人,内心充满着对未来的美好憧憬,期待自己能闯出属于自己的天地.更何况,作为一名程序员,无比的希望所有人对自己写得代码或者App ...