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 ...
随机推荐
- Android TextView 文字居中
有2种方法可以设置TextView文字居中: 一:在xml文件设置:android:gravity="center" 二:在程序中设置:m_TxtTitle.setGravity( ...
- ntelliJ IDEA 14 注册码
user or company nameo license key63625-MQ87K-3SRZ2-8MQYB-6NQZC-2Z8K6
- iOS:核心动画之转场动画CATransition
转场动画——CATransition CATransition是CAAnimation的子类,用于做转场动画,能够为层提供移出屏幕和移入屏幕的动画效果.iOS比Mac OS X的转场动画效果少一点 U ...
- jQuery-瀑布流-绝对定位布局(二)(延迟AJAX加载图片)
jQuery-瀑布流-绝对定位布局(二)(延迟AJAX加载图片) 瀑布流-绝对定位布局,与浮动布局的区别在于 1.布局不一样: 绝对定位:一个UL里面放置所有的绝对定位的LI: 浮动布局:多个(一 ...
- SQL Server ->> GROUPING SETS, CUBE, ROLLUP, GROUPING, GROUPING_ID
在我们制作报表的时候常常需要分组聚合.多组聚合和总合.如果通过另外的T-SQL语句来聚合难免性能太差.如果通过报表工具的聚合功能虽说比使用额外的T-SQL语句性能上要好很多,不过不够干脆,还是需要先生 ...
- Spring Loaded is a JVM agent for reloading class file changes
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot ...
- .NET + OpenCV & Python + OpenCV 配置
最近需要做一个图像识别的GUI应用,权衡了Opencv+ 1)QT,2)Python GUI,3).NET后选择了.NET... 本文给出C#+Opencv和Python+Opencv的相应参考,节省 ...
- 在Visual C++下搭建OpenGL的开发环境
1.确保你的电脑已经安装了visual c++编译器 如果还没安装的话,这里有个安装包,可以复制链接进行下载:http://pan.baidu.com/s/1bn4XTqn 2.下载GLUT 下载 ...
- Android之开发杂记(二)
1.android 4.0 以后 高版本没有虚拟菜单键
- dojo 十 ajax dojo/_base/xhr
官方教程:Ajax with DojoAjax功能:1.从服务器加载静态数据2.从web服务中访问xml或json类型的数据3.将表单(form)数据发送到服务器4.刷新页面内容....Ajax在RI ...