大意: 给定序列$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. nginx的ngx_str_t

    在nginx里的ngx_tr_t结构是字符串定义如下 typedef struct { size_t len; u_char *data; }ngx_str_t; 在给这样的结构体赋值的时候,ngin ...

  2. 轻松搭建ES6开发环境

    首先,你要自行查阅什么是ES6和ES5.javascript有什么关系,为什么要编译ES6.废话不多说,just go! 第一步:创建项目并让它成为npm可以管理的仓库. 新建一个项目,名字假设为te ...

  3. postgresql 字符串转整数 int、integer

    --把'1234'转成整数 select cast('1234' as integer ) ; --用substring截取字符串,从第8个字符开始截取2个字符:结果是12 select cast(s ...

  4. LC 926. Flip String to Monotone Increasing

    A string of '0's and '1's is monotone increasing if it consists of some number of '0's (possibly 0), ...

  5. 【Taro全实践】Taro在微信小程序中的生命周期

    一.Taro的本身生命周期 生命周期componentWillMount在微信小程序中这一生命周期方法对应页面的onLoad或入口文件app中的onLaunch componentDidMount在微 ...

  6. VBA 刷新数据透视表

    Sub pjCount() Dim r As Long r = Sheets("Inquery").[A65536].End(xlUp).Row ActiveSheet.Pivot ...

  7. 'pybot.bat' 不是内部或外部命令,也不是可运行的程序

    在通过命令行工具 运行RobotFramework的文件, 会使用到pybot.bat. 在dos输入pybot提示'pybot' 不是内部或外部命令,也不是可运行的程序或批处理文件, 可以在pyth ...

  8. C# WPF ASP.net 上传多文件和数据

    C# WinForm 上传多文件和数据 public static class HttpHelper { private static readonly Encoding DEFAULTENCODE ...

  9. 表单中使用<button>的注意点

    本文主要记录了我调查问题的思路想法,想看结论的同学直接拖到最后吧 上周在做项目的时候,有一个需求是在页面中加一个按钮,点一下查询数据库将内容填充在表格中.这不是很简单嘛,页面加个按钮,发送ajax请求 ...

  10. driver.switch_to.window(driver.window_handles[0])切换到最新打开窗口

    有时,使用selenium  定位页面元素时,浏览器明明打开的是需要定位的页面,但就是定位不到元素.打印一下元素page_source,会发现源码与页面不同. 主要问题是页面没有加载完成导致,需要设置 ...