Make It One CodeForces - 1043F (数论,最短路,好题)
大意: 给定序列$a$, 求最小子集, 使得gcd为1.
对于数$x$, 素因子多少次幂是无关紧要的, 这样就可以用一个二进制数来表示.
$x$取$gcd$后的二进制状态最多$2^7$, 可以暴力枚举后继$y$, 可以得到方案数为$sum=\sum\limits_{i=1}^n[gcd(a_i,x)=y]=\sum\limits_{d|\frac{x}{y}}\mu(d)cnt[yd]$.
($cnt[x]$为能被$x$整除的$a_i$个数).
若$sum>0$则可以达到这个后继. 这样跑一次$bfs$即可.
$bfs$的复杂度是A072047的三次幂求和, 打个表发现是N为3e5时只有5412256, 可以通过.
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, P2 = 998244353, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head const int N = 3e5+10;
int n, mu[N], gpf[N], d[N], cnt[N];
vector<int> fac[N], dv, val;
queue<int> q; void dfs(int d, int s, int num) {
val[s] = num;
if (d!=dv.size()) dfs(d+1,s,num),dfs(d+1,s|1<<d,num*dv[d]);
} void solve(int x) {
dv.clear();
int t = x;
while (t!=1) {
int z = gpf[t];
while (t%z==0) t/=z;
dv.pb(z);
}
int mx = 1<<dv.size();
val.resize(mx);
dfs(0,0,1);
REP(i,0,mx-1) {
int y = val[i];
int sum = 0, r = ~i&(mx-1);
for (int j=r; j; j=(j-1)&r) {
sum += mu[val[j]]*cnt[val[j]*y];
}
sum += mu[1]*cnt[y];
if (sum&&!d[y]) {
q.push(y);
d[y] = d[x]+1;
}
}
} int main() {
mu[1] = gpf[1] = 1;
REP(i,1,N-1) {
if (!gpf[i]) for (int j=i;j<N;j+=i) gpf[j]=i;
for (int j=i;j<N;j+=i) fac[j].pb(i);
for (int j=2*i;j<N;j+=i) mu[j]-=mu[i];
}
scanf("%d", &n);
REP(i,1,n) {
int t;
scanf("%d", &t);
set<int> s;
while (t!=1) s.insert(gpf[t]),t/=gpf[t];
for (int x:s) t*=x;
if (d[t]) continue;
d[t] = 1;
for (int x:fac[t]) ++cnt[x];
q.push(t);
}
if (d[1]) return puts("1"),0;
while (q.size()) {
int x = q.front(); q.pop();
solve(x);
}
printf("%d\n", d[1]?d[1]:-1);
}
Make It One CodeForces - 1043F (数论,最短路,好题)的更多相关文章
- Codeforces Round #378 (Div. 2) D题(data structure)解题报告
题目地址 先简单的总结一下这次CF,前两道题非常的水,可是第一题又是因为自己想的不够周到而被Hack了一次(或许也应该感谢这个hack我的人,使我没有最后在赛后测试中WA).做到C题时看到题目情况非常 ...
- poj1511/zoj2008 Invitation Cards(最短路模板题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Invitation Cards Time Limit: 5 Seconds ...
- Codeforces 828B Black Square(简单题)
Codeforces 828B Black Square(简单题) Description Polycarp has a checkered sheet of paper of size n × m. ...
- HDU 5521.Meeting 最短路模板题
Meeting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- hdu-3790最短路刷题
title: hdu-3790最短路刷题 date: 2018-10-20 14:50:31 tags: acm 刷题 categories: ACM-最短路 概述 一道最短路的水题,,,尽量不看以前 ...
- http://codeforces.com/gym/100623/attachments E题
http://codeforces.com/gym/100623/attachments E题第一个优化它虽然是镜像对称,但它毕竟是一一对称的,所以可以匹配串和模式串都从头到尾颠倒一下第二个优化,与次 ...
- [poj2449]Remmarguts' Date(K短路模板题,A*算法)
解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #includ ...
- 牛客小白月赛6 I 公交线路 最短路 模板题
链接:https://www.nowcoder.com/acm/contest/136/I来源:牛客网 题目描述 P市有n个公交站,之间连接着m条道路.P市计划新开设一条公交线路,该线路从城市的东站( ...
- http://codeforces.com/gym/100623/attachments H题
http://codeforces.com/gym/100623/attachments H题已经给出来的,包括后来添加的,都累加得到ans,那么从1-ans都是可以凑出来的,如果ans<a[n ...
随机推荐
- 【Spring Boot】 Spring Boot 2.x 版本 CacheManager 配置方式
Spring Boot 1.X RedisCacheManager 配置方式 @Bean public CacheManager cacheManager(RedisTemplate redisTem ...
- 前端知识点回顾之重点篇——CORS
CORS(cross origin resource sharing)跨域资源共享 来源:http://www.ruanyifeng.com/blog/2016/04/cors.html 它允许浏览器 ...
- PCL点云库(Point Cloud Library)简介
博客转载自:http://www.pclcn.org/study/shownews.php?lang=cn&id=29 什么是PCL PCL(Point Cloud Library)是在吸收了 ...
- Python类call函数的作用
call函数可以把类变成函数来调用call方法 class Demo(): def __init__(self, name): self.name = name def __call__(self): ...
- jsp细节------<base>
1:jsp一般都有这个<base href="<%=basePath%>">,它的作用一般用不到,但在使用java框架用注解时会用. 如下代码(xxx.js ...
- [CDH] Cloudera's Distribution including Apache Hadoop
You may choose to install spark, yarn, hive, etc one by one. [Spark] 00 - Install Hadoop & Spark ...
- pm2 使用
详见:https://www.cnblogs.com/chyingp/p/pm2-documentation.html
- IPv6 ping命令
IPv6 ping命令 一.Linux操作系统 给一台 Linux 主机分配了一个 IPv6 的 IP地址,如何使用 ping命令 确定该 IP地址 能否 ping 通呢? 1.查看主机的 IPv6 ...
- delphi ADOCONNECTION异常拦截
elphi ADOCONNECTION错误拦截错误框标题: Debugger Exception Notification内容: Project KJXX.exe raised excepti ...
- C++typedef的详细用法
转自知乎的一段解释: 作者:知乎用户链接:https://www.zhihu.com/question/29798061/answer/144423125来源:知乎著作权归作者所有.商业转载请联系作者 ...