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. Mysql之performance Schema

    Performance schema是用于监控Mysql执行,具有如下特征: 1.用于在运行时探查Mysql Server的执行过程,是由Performance_schema引擎和 Performan ...

  2. Mac 下 gradle 路径

    /Users/yourname/.gradle/wrapper/dists cmd:cd ~/.gradle/wrapper/dists/

  3. 再来一发!DB2 应用程序如何从数据库取数据 Fetch

    The FETCH statement positions a cursor on the next row of its result table and assigns the values of ...

  4. ListView优化中ViewHolder要不要定义为static静态内部类?

    给学生讲课的时候,发现存在这个问题,下来百度了下,发现很纠结,涉及到了内部类对外部类的引用,静态类的生命周期等java知识,现总结如下: static class ViewHolder { //定义l ...

  5. Linux文件系统(inode、block……)

    内容源于<鸟哥的Linux私房菜> 认识 EXT2 文件系统 文件系统的特殊观察与操作 文件系统 superblock,inode,block superblock,inode,block ...

  6. linux c学习笔记----互斥锁属性

    转自:http://lobert.iteye.com/blog/1762844 互斥锁属性 使用互斥锁(互斥)可以使线程按顺序执行.通常,互斥锁通过确保一次只有一个线程执行代码的临界段来同步多个线程. ...

  7. URAL 1966 Cycling Roads 点在线段上、线段是否相交、并查集

    F - Cycling Roads     Description When Vova was in Shenzhen, he rented a bike and spent most of the ...

  8. Arduino101学习笔记(七)—— 时间API

    1.毫秒时间 获取机器运行的时间长度, 单位毫秒. 系统最长的记录时间为9小时22分, 如果超出时间将从0开始. 警告: 时间为 unsigned long类型, 如果用 int 保存时间将得到错误结 ...

  9. android加载大量图片内存溢出的三种方法

    android加载大量图片内存溢出的三种解决办法 方法一:  在从网络或本地加载图片的时候,只加载缩略图. /** * 按照路径加载图片 * @param path 图片资源的存放路径 * @para ...

  10. 偷师--先留着。。ssh,nginx,防火墙相关命令