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

题意:

给出n个数和一个k,计算出至少要多少个数相乘才是k的倍数。

思路:
这道题目参考了杭电大神的代码http://blog.csdn.net/snowy_smile/article/details/52134304

对于每个数字,我们要么选,要么不选,这就很像01背包。但是肯定是需要预处理的。

对于每个数字,它所贡献的数就是它和k的最大公因数,这个不难理解吧。

所以我们可以把k的所有因子计算出来,因为有些因子很大,所以这里离散化一下,用map映射,这样后面才开得了数组。

我们用f[i][j]表示前i个数字,它们的gcd乘=映射j状态时的最佳方案。

当我们分析到第i个数字时,它所能贡献的数就是gcd(a[i],v[j]),那么它的前缀就是v[j]/gcd(a[i],v[j]),具体参见代码。

 #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<queue>
#include<cmath>
#include<map>
using namespace std;
typedef long long LL;
const int maxn=+; int n;
LL k;
LL s;
LL a[maxn];
LL b[maxn];
pair<int,LL> f[maxn][maxn*];
vector<LL> v;
map<LL,LL> key; LL gcd(LL a,LL b)
{
if(a<b) swap(a,b);
while(b!=)
{
LL t=a;
a=b;
b=t%b;
}
return a;
} void dp()
{
if(k==)
{
puts("");
LL tmp =0x3f3f3f3f3f3f3f3f, ans=-;
for (int i = ; i<=n;++i)
if (a[i]<=tmp)
{
ans=i;
tmp=a[i];
}
printf("%lld\n", ans);
return;
}
for(int j=;j<s;j++) f[][j]=make_pair(n+,);
for(int i=;i<=n;i++)
{
for (int j = ; j < s; ++j)
{
f[i][j] = f[i - ][j];
int pre = key[v[j] / gcd(v[j], b[i])];
f[i][j]=min(f[i][j], make_pair(f[i - ][pre].first + , f[i - ][pre].second + a[i]));
}
}
if (f[n][s-].first > n) puts("-1");
else
{
printf("%d\n", f[n][s-].first);
for (int i = n; i; --i)
{
if (f[i][key[k]] != f[i - ][key[k]])
{
printf("%d ", i);
k /= gcd(k, b[i]);
}
}
puts("");
}
} int main()
{
//freopen("D:\\input.txt","r",stdin);
while(~scanf("%d%lld",&n,&k))
{
LL m=sqrt(k+0.5);
v.clear();
for(int i=;i<=m;i++)
{
if(k%i==)
{
v.push_back(i);
if(k/i!=i) v.push_back(k/i);
}
}
sort(v.begin(),v.end());
s=v.size();
key.clear();
for(int i=;i<s;i++) key[v[i]]=i;
for(int i=;i<=n;i++)
{
scanf("%lld",&a[i]);
b[i]=gcd(k,a[i]);
}
dp();
}
return ;
}

Codeforces Round #365 (Div. 2) E - Mishka and Divisors(转化成01-背包)的更多相关文章

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

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

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

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

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

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

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

  5. Codeforces Round #365 (Div. 2) B - Mishka and trip

    http://codeforces.com/contest/703/problem/B 题意: 每个点都有一个值,每条边的权值为这两个点相乘.1~n成环.现在有k个城市,城市与其他所有点都相连,计算出 ...

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

    D. Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megabytes in ...

  7. Codeforces Round #365 (Div. 2) D.Mishka and Interesting sum

    题目链接:传送门 题目大意:给n个数,m次询问,每次询问区间 l,r 内出现偶数次数的异或和 题目思路:前缀和+离线处理+树状数组 首先可以知道, l,r 内出现奇数次的数的和,就是把 l,r内所有数 ...

  8. Codeforces Round #360 (Div. 2) E. The Values You Can Make 01背包

    题目链接: 题目 E. The Values You Can Make time limit per test:2 seconds memory limit per test:256 megabyte ...

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

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

随机推荐

  1. ubuntu14下创建软件的快捷启动方式

    下载软件,使用softname/bin/softname.sh即可启动,但是很麻烦,每次都要打开terminal 为了方便,我们需要创建desktop文件指向这个启动软件的shell文件(以创建Pyc ...

  2. Python程序员的10个常见错误(转)

    add by zhj:虽然学Python也有两年了,但这些问题的确容易犯,看来对Python的理解还有些地方不深入.先转了,有时间再好好看 译文:http://blog.jobbole.com/682 ...

  3. Shiro Annotation保护实例

    之前介绍了Shiro的Web訪问控制和缓存配置,事实上Shiro和Spring一样能够使用Annotation来配置方法级别的安全.省去自己每次获取Subject去做权限验证,比方在改动用户passw ...

  4. 使用selenium找出外卖点餐次数最多的10个顾客

    大锅在做外卖,给我说能否统计出这半年点餐次数最多的10个顾客,我不知道APP本身是否有这个功能,想了下最近用selenium较多,就用selenium尝试下吧. 1 定义一个类,这里先描述需要的属性和 ...

  5. Ubuntu 16.04 源码编译安装PHP7+swoole

    备注: Ubuntu 16.04 Server 版安装过程图文详解 Ubuntu16镜像地址: 链接:https://pan.baidu.com/s/1XTVS6BdwPPmSsF-cYF6B7Q 密 ...

  6. 接管radiobutton onclick 事件

    You can nil the OnClick event handler while changing a radiobutton state programmatically: procedure ...

  7. vuex是什么?怎么用,例子

    什么是vuex? 官方的解释是:Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化. 为什么要用 ...

  8. [转]Ubuntu使用Wireshark找不到interface的解决方法

    Wireshark是一款强大的有图形界面的网络封包分析工具. dumpcap需要root权限才能使用的,以普通用户打开Wireshark,Wireshark当然没有权限使用dumpcap进行截取封包. ...

  9. Pivot 和 Global 的一些总结

      相信大家一定有在 Unity 編輯器上看到這樣功能 這邊會跟大家說明這項功能有什麼用處     Pivot  意思為,輔助編輯的工具標示,會顯示在第一個選取的物件身上. Center 意思為,輔助 ...

  10. qemu-nbd方式挂载qcow2镜像

    客户端配置 加载nbd模块 [root@centos sm]# rmmod nbd [root@centos sm]# modprobe nbd max_part=8 映射服务器的块设备到本地nbd设 ...