大意: 给定$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, 可以优化一下....

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstdio>
  4. #include <math.h>
  5. #include <set>
  6. #include <map>
  7. #include <queue>
  8. #include <string>
  9. #include <string.h>
  10. #include <bitset>
  11. #define REP(i,a,n) for(int i=a;i<=n;++i)
  12. #define PER(i,a,n) for(int i=n;i>=a;--i)
  13. #define hr putchar(10)
  14. #define pb push_back
  15. #define lc (o<<1)
  16. #define rc (lc|1)
  17. #define mid ((l+r)>>1)
  18. #define ls lc,l,mid
  19. #define rs rc,mid+1,r
  20. #define x first
  21. #define y second
  22. #define io std::ios::sync_with_stdio(false)
  23. #define endl '\n'
  24. using namespace std;
  25. typedef long long ll;
  26. typedef pair<int,ll> pii;
  27. const int P = 1e9+7, INF = 0x3f3f3f3f;
  28. ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
  29. 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;}
  30. ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
  31. //head
  32.  
  33. const int N = 1e3+10, M = 7e3+10;
  34. int n, sz;
  35. ll k, a[N], b[N];
  36. vector<ll> A;
  37. map<ll,int> S;
  38. pii dp[N][M];
  39.  
  40. int main() {
  41. scanf("%d%lld", &n, &k);
  42. REP(i,1,n) scanf("%lld", a+i),b[i]=gcd(a[i],k);
  43. if (k==1) return printf("1\n%d\n",int(min_element(a+1,a+1+n)-a)),0;
  44. int mx = sqrt(k+0.5);
  45. REP(i,1,mx) if (k%i==0) {
  46. A.pb(i);
  47. if (k/i!=i) A.pb(k/i);
  48. }
  49. sort(A.begin(),A.end());
  50. sz = A.size();
  51. REP(i,0,sz-1) S[A[i]]=i;
  52. REP(i,1,sz-1) dp[0][i]=pii(n+1,0);
  53. REP(i,1,n) REP(j,0,sz-1) {
  54. ll pre = S[A[j]/gcd(A[j],b[i])];
  55. dp[i][j] = pii(dp[i-1][pre].x+1,dp[i-1][pre].y+a[i]);
  56. dp[i][j] = min(dp[i][j], dp[i-1][j]);
  57. }
  58. if (dp[n][sz-1].x==n+1) return puts("-1"),0;
  59. printf("%d\n", dp[n][sz-1].x);
  60. PER(i,1,n) if (dp[i][S[k]]!=dp[i-1][S[k]]) {
  61. printf("%d ", i);
  62. k /= gcd(k,b[i]);
  63. } hr;
  64. }

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. kafka存储数据量过大,导致磁盘爆满

    问题: 注意到自己负责kafka的某个topic最小的偏移量为0,而最大的偏移量都7亿多了,说明存储在kafka里面的数据没有定时删除,通过登陆到kafka服务器,查看配置文件services.pro ...

  2. 怎样把QQ群降级(1000人降到200或500人,500人降到200)

    怎样把QQ群降级(1000人降到200或500人,500人降到200)QQ群只有升级的选项,没有降级选项,一旦升级为1000人,就无法直接降级为200人或500人,建群时选择了500人也无法降到200 ...

  3. Struts2快速后台验证 使用

    为了和前台基于JavaScript的开源验证框架RapidValidation使用统一的验证规则, 最大限度的减少重复的后台验证代码, 使用方式简便, 扩展方便. https://blog.csdn. ...

  4. JAVA学习调查问卷——20145101

    1.你对自己的未来有什么规划?做了哪些准备? 我希望在未来不管自己是否从事机要工作,都要做一个有能力,对社会能有所贡献的人.所以在现阶段我应该努力学习基础知识,夯实基本功,具备成为合格机要人的素质. ...

  5. 20145331魏澍琛《网络对抗》Exp4 恶意代码分析

    20145331魏澍琛<网络对抗>Exp4 恶意代码分析 基础问题回答 1.如果在工作中怀疑一台主机上有恶意代码,但只是猜想,所有想监控下系统一天天的到底在干些什么.请设计下你想监控的操作 ...

  6. tf.multiply()和tf.matmul()区别

    (1)tf.multiply是点乘,即Returns x * y element-wise. (2)tf.matmul是矩阵乘法,即Multiplies matrix a by matrix b, p ...

  7. __NSCFConstantString && __NSPlaceholderDictionary

    一 -[__NSCFConstantString size]: unrecognized selector sent to instance 0x53ea70 该错误是在我将NSString类型的参数 ...

  8. 简谈高通Trustzone的实现【转】

    本文转载自:https://blog.csdn.net/hovan/article/details/42520879 从trust zone之我见知道,支持trustzone的芯片会跑在两个世界. 普 ...

  9. linux下命令行工具gcp显示拷贝进度条

    1.环境: ubuntu16.04 Linux jello 4.4.0-89-generic #112-Ubuntu SMP Mon Jul 31 19:38:41 UTC 2017 x86_64 x ...

  10. POJ 3468 A Simple Problem with Integers(线段树&区间更新)题解

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...