大意: 给定序列$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 (数论,最短路,好题)的更多相关文章

  1. Codeforces Round #378 (Div. 2) D题(data structure)解题报告

    题目地址 先简单的总结一下这次CF,前两道题非常的水,可是第一题又是因为自己想的不够周到而被Hack了一次(或许也应该感谢这个hack我的人,使我没有最后在赛后测试中WA).做到C题时看到题目情况非常 ...

  2. poj1511/zoj2008 Invitation Cards(最短路模板题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Invitation Cards Time Limit: 5 Seconds    ...

  3. Codeforces 828B Black Square(简单题)

    Codeforces 828B Black Square(简单题) Description Polycarp has a checkered sheet of paper of size n × m. ...

  4. HDU 5521.Meeting 最短路模板题

    Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  5. hdu-3790最短路刷题

    title: hdu-3790最短路刷题 date: 2018-10-20 14:50:31 tags: acm 刷题 categories: ACM-最短路 概述 一道最短路的水题,,,尽量不看以前 ...

  6. http://codeforces.com/gym/100623/attachments E题

    http://codeforces.com/gym/100623/attachments E题第一个优化它虽然是镜像对称,但它毕竟是一一对称的,所以可以匹配串和模式串都从头到尾颠倒一下第二个优化,与次 ...

  7. [poj2449]Remmarguts' Date(K短路模板题,A*算法)

    解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #includ ...

  8. 牛客小白月赛6 I 公交线路 最短路 模板题

    链接:https://www.nowcoder.com/acm/contest/136/I来源:牛客网 题目描述 P市有n个公交站,之间连接着m条道路.P市计划新开设一条公交线路,该线路从城市的东站( ...

  9. http://codeforces.com/gym/100623/attachments H题

    http://codeforces.com/gym/100623/attachments H题已经给出来的,包括后来添加的,都累加得到ans,那么从1-ans都是可以凑出来的,如果ans<a[n ...

随机推荐

  1. JavaScript判断数据类型的4中方法

    一: typeof typeof 是一种运算符,它的值有如下几种(number.boolean.string.undefined.null.function.object.symbol) consol ...

  2. TIZ_c 第0周总结(2019/10/15-2019/10/22)工欲善其事必先利其器

    TIZ_c 第0周总结(2019/10/15-2019/10/22)工欲善其事必先利其器 任务清单 给自己取一个酷酷的id,并选择1-2个喜欢的方向.(只是初步选择,后期可更改) 改下群名片.例如yo ...

  3. How to get full path of StreamWriter

     How to get full path of StreamWriter   In my version of the framework, this seems to work: string f ...

  4. smaller programs should improve performance RISC(精简指令集计算机)和CISC(复杂指令集计算机)是当前CPU的两种架构 区别示例

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION In this section, we l ...

  5. 前端三大框架(Angular Vue React)

    前端,HTML(超文本标记语言),CSS(层叠样式表)和JavaScript(脚本语言) HTML,通常说的h5,其实按标准来说,HTML4的后续版本不带编号了,并保证向前的兼容性 CSS的版本3,增 ...

  6. 机器学习 - 算法 - 集成算法 - 分类 ( Bagging , Boosting , Stacking) 原理概述

    Ensemble learning - 集成算法 ▒ 目的 让机器学习的效果更好, 量变引起质变 继承算法是竞赛与论文的神器, 注重结果的时候较为适用 集成算法 - 分类 ▒ Bagging - bo ...

  7. PHP格式化数字和SMARTY格式化数字的方法

    PHP格式化: $num="3";$format="%06d";  //6是位数,这里有6位数,0是不足6位的补0$a=sprintf($format,$num ...

  8. RabbitMQ学习之:(八)Topic Exchange (转贴+我的评论)

    From: http://lostechies.com/derekgreer/2012/05/18/rabbitmq-for-windows-topic-exchanges/ RabbitMQ for ...

  9. mac被锁有pin码的解锁方法

    停用规律: 错误5次密码停用1分钟 再错误3次停用5分钟 在错误1次就停用15分钟 再错误1次就是60分钟了,而且还没输入框了,这时候我们要通过按 option,commond这2个按钮来唤起输入框 ...

  10. MySQL数据库同步工具的设计与实现

    一.背景 在测试过程中,对于不同的测试团队,出于不同的测试目的,我们可能会有多套测试环境.在产品版本迭代过程中,根据业务需求,会对数据库的结构进行一些修改,如:新增表.字段.索引,修改表.字段索引等操 ...