http://codeforces.com/contest/703/problem/E

题意:给定一个最多个数的序列,从中选出最少个数的数字,使得他们的乘积是k的倍数,若有多种选择方式,输出选出数字和最小的一种,若有多种,输出任意一种。

动态规划,dp[i][j]表示从前i个数里选,所得乘积是j的倍数。显然dp[i][j]=max(dp[i-1][j],dp[i-1][j/gcd(j,a[i])])。由于k可能很大,所以只需令j分别等于k的每个约数即可。

确定k的约数的时候令i从1到sqrt(k)循环判断,注意由于k很大,这里的i要使用longlong。

#include<bits/stdc++.h>
using namespace std;
#define ft first
#define sd second
#define mp make_pair
long long a[], k, f[], b[];
int fc, n;
pair<int, long long> dp[][];
map<long long , int> e;
int main()
{
//freopen("input.txt", "r", stdin);
scanf("%d%I64d", &n, &k);
long long t = k;
for (int i = ; i <= n; i++)
{
scanf("%I64d", &a[i]);
b[i] = __gcd(k, a[i]);
t /= __gcd(t, a[i]);
}
if (t != )
{
printf("-1\n");
return ;
}
if (k == )
{
printf("1\n%d\n", (int)(min_element(a + , a + n + ) - a));
return ;
}
e.clear();
fc = ;
for (long long i = ; i * i <= k; i++)
{
if (k % i != ) continue;
f[fc++] = i;
if (i * i != k) f[fc++] = k / i;
}
sort(f, f + fc);
for (int i = ; i < fc; i++)
e[f[i]] = i;
for (int i = ; i < fc; i++)
dp[][i] = mp(n + , );
dp[][] = mp(, );
for (int i = ; i <= n; i++)
for (int j = ; j < fc; j++)
{
dp[i][j] = dp[i - ][j];
long long v = e[f[j] / __gcd(f[j], b[i])];
if (dp[i][j] > mp(dp[i - ][v].ft + , dp[i - ][v].sd + a[i]))
dp[i][j] = mp(dp[i - ][v].ft + , dp[i - ][v].sd + a[i]);
}
printf("%d\n", dp[n][fc - ].ft);
t = k;
for (int i = n; i > ; i--)
{
if (dp[i][e[t]] == dp[i - ][e[t]]) continue;
printf("%d ", i);
t /= __gcd(t, b[i]);
}
printf("\n");
//fclose(stdin);
return ;
}

Mishka and Divisors[CodeForces Round #365 Div.2]的更多相关文章

  1. Codeforces Round #365 (Div. 2) C - Chris and Road 二分找切点

    // Codeforces Round #365 (Div. 2) // C - Chris and Road 二分找切点 // 题意:给你一个凸边行,凸边行有个初始的速度往左走,人有最大速度,可以停 ...

  2. Codeforces Round #365 (Div. 2) A 水

    A. Mishka and Game time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  3. Codeforces Round #365 (Div. 2) A

    Description Mishka is a little polar bear. As known, little bears loves spending their free time pla ...

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

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

  5. 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 ...

  6. Codeforces Round #365 (Div. 2)-D Mishka and Interesting sum(树状数组)

    题目链接:http://codeforces.com/contest/703/problem/D 思路:看了神犇的代码写的... 偶数个相同的数异或结果为0,所以区间ans[l , r]=区间[l , ...

  7. Codeforces Round #365 (Div. 2) Mishka and trip

    Mishka and trip 题意: 有n个城市,第i个城市与第i+1个城市相连,他们边的权值等于i的美丽度*i+1的美丽度,有k个首都城市,一个首都城市与每个城市都相连,求所有边的权值. 题解: ...

  8. Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum (离线树状数组+前缀xor)

    题目链接:http://codeforces.com/contest/703/problem/D 给你n个数,m次查询,每次查询问你l到r之间出现偶数次的数字xor和是多少. 我们可以先预处理前缀和X ...

  9. Codeforces Round #365 (Div. 2) D - Mishka and Interesting sum(离线树状数组)

    http://codeforces.com/contest/703/problem/D 题意: 给出一行数,有m次查询,每次查询输出区间内出现次数为偶数次的数字的异或和. 思路: 这儿利用一下异或和的 ...

随机推荐

  1. 按键的使用(一)------verilog

    按键在项目中应用还是很频繁的,这里主要介绍按键的几种用法. 1.按下一次有效:按下一次计数器增加一下. 2.按下连续有效:按下不松,计数器就一直增加. 3.按下无效,松开有效:按下时计数器值不变,按键 ...

  2. Android Programming: Pushing the Limits -- Chapter 7:Android IPC -- Messenger

    Messenger类实际是对Aidl方式的一层封装.本文只是对如何在Service中使用Messenger类实现与客户端的通信进行讲解,对Messenger的底层不做说明.阅读Android Prog ...

  3. SQLServer索引

    SQLServer索引1.聚集和非聚集索引聚集索引:根据聚集索引进行排序,非聚集索引因为不根据索引键排序,所以聚集索引比非聚集索引快(一个表只有一个聚集索引)2.唯一索引和非唯一索引唯一索引时值不能重 ...

  4. 重温WCF之WCF传输安全(十三)(4)基于SSL的WCF对客户端采用证书验证(转)

    转载地址:http://www.cnblogs.com/lxblog/archive/2012/09/20/2695397.html 前一篇我们演示了基于SSL的WCF 对客户端进行用户名和密码方式的 ...

  5. 动态生成SQL执行语句

    SET @qry = 'SELECT product_cd, name, product_type_cd, date_offered, date_retired FROM product WHERE ...

  6. hdu 1698:Just a Hook(线段树,区间更新)

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  7. android 入门-库的生成jar 和引用jar

    开发环境 1.eclipse 2.android studio 步骤 1.在eclipse 生成 jar包 2.在android studio 引用 jar包 实现步骤 1.在eclipse 创建一个 ...

  8. 智能车学习(二十)——浅谈C车硬连接与软连接

    一.为何要追求软连接?       车子进行软连接之后,可以达到一种效果,就是在高速过程中,车子如果快要发生侧翻的时候,只会跳一个后轮,且只是轻微,而前轮如果进行的内倾,就可以让前轮最大面积接触,增大 ...

  9. cocos2dx游戏开发——微信打飞机学习笔记(一)——开发准备

    一.环境的搭建 1.Windows开发准备: (1)软件下载及安装 •下载Cocos2d-x 最新版本:http://www.cocos2d-x.org/download 或者从Cocos2d-x G ...

  10. 【spring bean】 spring中bean之间的引用以及内部bean

    在spring中会有如下的几种情况: 1.在当前容器中,(即在spring.xml这一个配置文件中),一个bean引用了另一个bean. 使用 1>  <ref  bean="另 ...