分析:怎么看都是超时,但是可以先筛一遍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. 【总结】杂谈Java异常处理

    软件开发中一个古老的说法是:80%的工作使用20%的时间.80%是指检查和处理错误所付出的努力.在许多语言中,编写检查和处理错误的程序代码很乏味,并使应用程序代码变得冗长.原因之一就是它们的错误处理方 ...

  2. 唯一区别是不会去取emptyText 的值,没有选选择选项的时候返回是空字符串

    combox取值以及赋值的方法 function getValue() { //注意:以下这两种取值方法都会存在一个问题: 当combox设置成能输入并有只能提示的时候,当输入的不是备选项时,或到的v ...

  3. linux环境下,利用tc限制两台服务器间的网速,非常简单。

    最近再搞postgres的数据同步,需要模拟异地机房有带宽限制时的同步效果,所以想要限制一下两台机器之间的网速. ts命令功能很强,同时也好难理解和使用,经常浪费了好半天还是搞不定. 这里分享一个简单 ...

  4. URAL 1233 Amusing Numbers 好题

    参照了nocow上的解法,照搬过来…… 易知一个数X在数列中在另一个数Y前,当且仅当X前缀小于Y或前缀相等X短,那么我们分布考虑,比如对于数48561: 5位上:10000~48560; 4位上:10 ...

  5. android AsyncTask异步下载并更新进度条

    AsyncTask异步下载并更新进度条    //如果不是很明白请看上篇文章的异步下载 AsyncTask<String, Integer, String> 第一个参数:String 传入 ...

  6. Linux之守护进程

    一.守护进程概述 在linux或者unix操作系统中在系统的引导的时候会开启很多服务,这些服务就叫做守护进 程.为了增加灵活性,root可以选择系统开启的模式,这些模式叫做运行级别,每一种运行级别以一 ...

  7. android sqlite 一次创建多个表

    package com.yangguangfu.database; import android.content.Context; import android.database.sqlite.SQL ...

  8. Android gingerbread eMMC booting

    Android gingerbread eMMC booting This page is currently under construction. The content of this page ...

  9. grep/awk/sed 或者 并且 否定

    Grep 'OR' Operator Find all the lines in a file, that match any of the following patterns. Using GRE ...

  10. staging server, source congtrol, deply workflow using git

    web项目开发中,有三个实践对于项目成功是非常重要的: 1. staging servers 2. Version control workflows 3. Tested, repeatable de ...