Longest Subsequence codeforces 632D 暴力数学
来源:互联网 发布:魔域单机登录器源码 编辑:程序博客网 时间:2024/05/21 12:00
ou are given array a with n elements and the number m. Consider some subsequence of a and the value of least common multiple (LCM) of its elements. Denote LCM as l. Find any longest subsequence of a with the value l ≤ m.
A subsequence of a is an array we can get by erasing some elements of a. It is allowed to erase zero or all elements.
The LCM of an empty array equals 1.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 106) — the size of the array a and the parameter from the problem statement.
The second line contains n integers ai (1 ≤ ai ≤ 109) — the elements of a.
Output
In the first line print two integers l and kmax (1 ≤ l ≤ m, 0 ≤ kmax ≤ n) — the value of LCM and the number of elements in optimal subsequence.
In the second line print kmax integers — the positions of the elements from the optimal subsequence in the ascending order.
Note that you can find and print any subsequence with the maximum length.
Example
Input
7 8
6 2 9 2 7 2 3
Output
6 5
1 2 4 6 7
Input
6 4
2 2 2 3 3 3
Output
2 3
1 2 3
题意:求一个最长子序列使得序列中的数的lcm小于m
分析:因为m的大小为 1e6.
所以可以暴力 最小公倍数肯定是比得到的序列中的任何一个数都要小,所以i把能到的倍数加上i的数量就好
#include <bits/stdc++.h>using namespace std;const int maxn=1e6+10;int a[maxn];int num[maxn];int sum[maxn];int main(){ int n,m; cin>>n>>m; for(int i=1;i<=n;i++) { scanf("%d",&a[i]); if(a[i]<=m) num[a[i]]++; } for(int i=1;i<=m;i++) { if(num[i]) { for(int j=i;j<=m;j+=i) sum[j]+=num[i]; } } int maxx=0,maxval=0; for(int i=1;i<=m;i++) { if(sum[i]&&maxx<sum[i]) { maxval=i; maxx=sum[i]; } } if(maxx==0) printf("1 0\n"); else { int cnt=0; printf("%d %d\n",maxval,maxx ); for(int i=1;i<=n;i++) { if(maxval%a[i]==0) { if(cnt) printf(" "); printf("%d",i ); ++cnt; } } printf("\n"); }}
- Longest Subsequence codeforces 632D 暴力数学
- Longest Subsequence CodeForces 632D 数学/筛法
- Codeforces-632D Longest Subsequence
- Codeforces 632D Longest Subsequence
- CodeForces 632D Longest Subsequence(数论)
- [杂题] Codeforces #632D Longest Subsequence
- Codeforces 632D Longest Subsequence 【求因子和变形】
- Codeforces 632D 暴力
- Codeforces 633D Fibonacci-ish 数学+暴力
- Educational Codeforces Round 9(D. Longest Subsequence(筛法))
- Educational Codeforces Round 9-D. Longest Subsequence(筛选法)
- Educational Codeforces Round 9 D. Longest Subsequence CF632D
- CF 632D(Longest Subsequence-计数排序)
- D. Longest Subsequence
- codeforces Longest Increasing Subsequence
- codeForces-Longest Subsequence
- Longest Subsequence CodeForces
- cf#ecr9-D - Longest Subsequence
- postfix+mysql
- AtCoder:3N Numbers(优先队列)
- pca
- zlib库使用简单讲解
- windows下安装pip
- Longest Subsequence codeforces 632D 暴力数学
- java多线程的常见例子
- XML约束
- Shell Cookbook
- Magic Numbers codeforces 628D 数位dp
- 实现批量删除(物理删除)
- Java多线程-实例解析
- 程序员职业素养 读书笔记
- MyBatis级联多条件分页查询