大意: 给定$n$个数, 求选择最少的数满足积为$k$的倍数, 并且和最小

刚开始想着暴力维护$k$的素因子向量, 用map转移, 结果T了. 看了下别的dala0题解, 不需要考虑素因子, 我们考虑k的所有因子, 用map预处理一下每个因子再转移就好了.

总的复杂度是$O(n\sigma_0(k)logk)$, 1e12以内除数函数最大值是6720, 应该是可以过的, 但这题太卡时限了, long long 的gcd跑太慢. 但是可以发现每次只对k求gcd, 可以优化到$O(n\sigma_0(k)primes(k))$, 或者提前对a数组取一下gcd, 可以优化一下....

#include <iostream>
#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'
using namespace std;
typedef long long ll;
typedef pair<int,ll> pii;
const int P = 1e9+7, 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;}
//head const int N = 1e3+10, M = 7e3+10;
int n, sz;
ll k, a[N], b[N];
vector<ll> A;
map<ll,int> S;
pii dp[N][M]; int main() {
scanf("%d%lld", &n, &k);
REP(i,1,n) scanf("%lld", a+i),b[i]=gcd(a[i],k);
if (k==1) return printf("1\n%d\n",int(min_element(a+1,a+1+n)-a)),0;
int mx = sqrt(k+0.5);
REP(i,1,mx) if (k%i==0) {
A.pb(i);
if (k/i!=i) A.pb(k/i);
}
sort(A.begin(),A.end());
sz = A.size();
REP(i,0,sz-1) S[A[i]]=i;
REP(i,1,sz-1) dp[0][i]=pii(n+1,0);
REP(i,1,n) REP(j,0,sz-1) {
ll pre = S[A[j]/gcd(A[j],b[i])];
dp[i][j] = pii(dp[i-1][pre].x+1,dp[i-1][pre].y+a[i]);
dp[i][j] = min(dp[i][j], dp[i-1][j]);
}
if (dp[n][sz-1].x==n+1) return puts("-1"),0;
printf("%d\n", dp[n][sz-1].x);
PER(i,1,n) if (dp[i][S[k]]!=dp[i-1][S[k]]) {
printf("%d ", i);
k /= gcd(k,b[i]);
} hr;
}

Mishka and Divisors CodeForces - 703E的更多相关文章

  1. Mishka and Divisors[CodeForces Round #365 Div.2]

    http://codeforces.com/contest/703/problem/E 题意:给定一个最多个数的序列,从中选出最少个数的数字,使得他们的乘积是k的倍数,若有多种选择方式,输出选出数字和 ...

  2. codeforces 703E Mishka and Divisors

    codeforces 703E Mishka and Divisors 题面 给出大小为\(1000\)的数组和一个数\(k\),求长度最短的一个子序列使得子序列的元素之积是\(k\)的倍数,如果有多 ...

  3. Common Divisors CodeForces - 182D || kmp最小循环节

    Common Divisors CodeForces - 182D 思路:用kmp求next数组的方法求出两个字符串的最小循环节长度(http://blog.csdn.net/acraz/articl ...

  4. Codeforces Round #365 (Div. 2) E - Mishka and Divisors(转化成01-背包)

    http://codeforces.com/contest/703/problem/E 题意: 给出n个数和一个k,计算出至少要多少个数相乘才是k的倍数. 思路:这道题目参考了杭电大神的代码http: ...

  5. B - Common Divisors (codeforces)数论算法基本定理,唯一分解定理模板

    You are given an array aa consisting of nn integers. Your task is to say the number of such positive ...

  6. Codeforces 703E DP + 因数分解 +离散化

    题意:给你n个数,和一个数m, 问最小需要多少个数,可以让这些数乘起来是m的倍数.如果有多组,取和最小的那一组. 思路:因为m的范围到1e12,并且和取模相关,所以容易想到处理出m的约数,然后离散化一 ...

  7. Common Divisors CodeForces - 1203C

    题意: 给你n个数,让你找出来公因子有多少个.公因子:对于这n个数,都能被这个公因子整除 题解: 只需要找出来这n个数的最大公因子x,然后找出来有多少不同数能把x给整.(因为我们可以保证x可以把这n个 ...

  8. codeforces703B

    Mishka and trip CodeForces - 703B 小米什卡是一个伟大的旅行者,她访问了许多国家.在这次考虑去哪里旅行之后,她选择了XXX--这个美丽,但鲜为人知的北方国家. 以下是关 ...

  9. Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum 离线+线段树

    题目链接: http://codeforces.com/contest/703/problem/D D. Mishka and Interesting sum time limit per test ...

随机推荐

  1. 【Python】【Flask】前端调用后端方法

    后端代码: @app.route("/test",methods=['POST','GET']) def test(): return "我是测试的" 前端代码 ...

  2. Python安装selenium,配置火狐浏览器环境

    想用Python去编写自动化脚本进行网页访问时,遇到了一些问题, File "C:\Python34\lib\site-packages\selenium-3.0.0b2-py3.4.egg ...

  3. Python之路----列表推导式和生成器的表达式

    列表推导式 egg_list=['鸡蛋%s'%i for i in range(10)] print(egg_list) 列表推导式 推导过程 egg_list = [] for i in range ...

  4. TED #10# A rite of passage for late life

    Bob Stein: A rite of passage for late life Collection I grew up white, secular and middle class in 1 ...

  5. ES6学习--对象属性的可枚举性( enumerable)

    可枚举性(enumerable)用来控制所描述的属性,是否将被包括在for...in循环之中.具体来说,如果一个属性的enumerable为false,下面三个操作不会取到该属性.* for..in循 ...

  6. Python3 Selenium自动化-select下拉框

    Python3 Selenium自动化-select下拉框 selenium介绍select下拉框相关的操作方法:

  7. mysql主备切换[高可用]

    到这一步的时候, 是主备部署已经处理好, 请关注:mysql主备部署[高可用] 这次使用的是keepalived-1.2.22.tar.gz版, 官网地址:keeplived官网 笼统知识请自行查询百 ...

  8. 20145216《网络对抗》逆向及BOF基础实践

    20145216<网络对抗>逆向及BOF基础实践 1 逆向及Bof基础实践说明 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件.该程序正常执行流程是:main调用foo函 ...

  9. 20145302张薇 《网络对抗技术》 web基础

    20145302张薇 <网络对抗> web基础 实验问题回答 1.什么是表单 表单在网页中主要负责数据采集功能:一般网页上需要用户输入.选择的地方都会用到表单 表单标签:即,用于确定表单所 ...

  10. 20145322 Exp5 MS08_067漏洞测试

    20145322何志威 Exp5 MS08_067漏洞测试 实验过程 kali ip:172.20.10.4 windows 2003 ip:172.20.10.2 在控制台内使用search ms0 ...