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 ...
随机推荐
- linux驱动模型<输入子系统>
在linux中提供一种输入子系统的驱动模型,其主要是实现在input.c中. 在输入子系统这套模型中,他把驱动分层分类.首先分为上下两层,上层为input.c .下层为驱动的实现,下层分为两部分,一部 ...
- linux read和write函数
原文出处:http://blog.chinaunix.net/space.php?uid=20558494&do=blog&id=2803003read函数是Linux下不带缓存的文件 ...
- 【USACO】【section1.1】Your Ride Is Here
以前的账号忘记了,只能从头了. 入门题不解释,就是sumg和sumc初始值置1不能置0.开始享用一个循环计算出sumg和sumc,其实两个数组最大程度为6,节省不了什么时间. /*ID:Moment1 ...
- Linux内核同步机制之(三):memory barrier【转】
转自:http://www.wowotech.net/kernel_synchronization/memory-barrier.html 一.前言 我记得以前上学的时候大家经常说的一个词汇叫做所见即 ...
- CMD下查看某个端口被谁占用了
cmd运行 netstat -aon|findstr "8080" 运行结果如下 TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING 6416 TCP [:: ...
- 路由器扫描的Java源码
这个源码不是本人写的,是我原来的领导写的,我们都叫他东哥,这个是东留给我的一个小资源,好佩服他哦,这个东西可以用来扫描全世界的路由器,破解路由器账户和密码 当然是简单的了.我能力不够没有更完善的补充下 ...
- 2.cadence制板流程[原创]
1.元器件库(原理图库) 2.原理图 3.DRC检查 4.输出网表 5.PCB封装 6.板子边框 7.导入网表 8.设置约束规则 9.布局,布线,铺铜 10.DRC检查,出丝印,钻孔,出广汇
- ZOJ Problem Set - 3861 Valid Pattern Lock(dfs)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3861 这道题当时没做出来,后来经过队友提醒才做出来. 3*3的九宫格,给你 ...
- Vector示例一,二
#include <iostream> int main(void) { double a[] = {1, 2, 3, 4, 5}; std::cout<<mean(a, 5) ...
- laravel named route
laravel中一般对于路由的使用方法是在routes.php中定义一个路由,在view中如果要引用一个url则直接通过<a href="url/">来使用. 但是随着 ...