Mishka and Divisors[CodeForces Round #365 Div.2]
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]的更多相关文章
- Codeforces Round #365 (Div. 2) C - Chris and Road 二分找切点
// Codeforces Round #365 (Div. 2) // C - Chris and Road 二分找切点 // 题意:给你一个凸边行,凸边行有个初始的速度往左走,人有最大速度,可以停 ...
- 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 ...
- Codeforces Round #365 (Div. 2) A
Description Mishka is a little polar bear. As known, little bears loves spending their free time pla ...
- Codeforces Round #365 (Div. 2) E - Mishka and Divisors(转化成01-背包)
http://codeforces.com/contest/703/problem/E 题意: 给出n个数和一个k,计算出至少要多少个数相乘才是k的倍数. 思路:这道题目参考了杭电大神的代码http: ...
- 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 ...
- Codeforces Round #365 (Div. 2)-D Mishka and Interesting sum(树状数组)
题目链接:http://codeforces.com/contest/703/problem/D 思路:看了神犇的代码写的... 偶数个相同的数异或结果为0,所以区间ans[l , r]=区间[l , ...
- Codeforces Round #365 (Div. 2) Mishka and trip
Mishka and trip 题意: 有n个城市,第i个城市与第i+1个城市相连,他们边的权值等于i的美丽度*i+1的美丽度,有k个首都城市,一个首都城市与每个城市都相连,求所有边的权值. 题解: ...
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum (离线树状数组+前缀xor)
题目链接:http://codeforces.com/contest/703/problem/D 给你n个数,m次查询,每次查询问你l到r之间出现偶数次的数字xor和是多少. 我们可以先预处理前缀和X ...
- Codeforces Round #365 (Div. 2) D - Mishka and Interesting sum(离线树状数组)
http://codeforces.com/contest/703/problem/D 题意: 给出一行数,有m次查询,每次查询输出区间内出现次数为偶数次的数字的异或和. 思路: 这儿利用一下异或和的 ...
随机推荐
- Struts2之OGNL
一.OGNL是什么? OGNL(Object-Graph Navigation Language)对象图导航语言,是一种表达式语言,它可以 1.遍历对象的结构图 2.存取对象的属性(实例属性和静态属性 ...
- hadoop 2.5 hdfs namenode –format 出错Usage: java NameNode [-backup] |
在 cd /home/hadoop/hadoop-2.5.2/bin 下 执行的./hdfs namenode -format 报错[hadoop@node1 bin]$ ./hdfs nameno ...
- Page Object Model (Selenium, Python)
时间 2015-06-15 00:11:56 Qxf2 blog 原文 http://qxf2.com/blog/page-object-model-selenium-python/ 主题 Sel ...
- JavaWeb学习之转发和重定向、会话技术:cookie、session、验证码实例、URLConnection使用(下载网页)(4)
1.转发和重定向 HttpServletResponse response 转发: RequestDispatcher dispatcher = request.getRequestDispatche ...
- WMSYS.WM_CONCAT 函數的用法
select t.rank, t.Name from t_menu_item t; 10 CLARK 10 KING 10 MILLER 20 ADAMS 20 FORD ...
- [LeetCode] Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- Spring XML配置文件示例(一)——<Servlet name>-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- MyEclipse 选中右侧编辑的文件时自动展开左侧目录树
- play-framework的安装与使用
一.下载: 到http://www.playframework.com/download下载 解压好包,然后输入: activator ui 访问:http://127.0.0.1:8888/home
- IOS登陆+注册+抽奖+排行榜
要求:三个页面(登录页面,pickerView页面,排行榜页面),pickerView页面是三个组件,每个组件显示0-9,点击按钮进行随机,获得的值存入排行榜,排行榜显示大于500的最高的10个分数和 ...