首先我们能注意到两个数x, y (0 < x , y < m) 乘以倍数互相可达当且仅当gcd(x, m) == gcd(y, m)

然后我们可以发现我们让gcd(x, m)从1开始出发走向它的倍数一个一个往里加元素就好啦, 往那边走

这个可以用dp求出来, dp[ i ] 表示 gcd(x, m)从 i 开始最大元素一共有多少个, dp[ i ] = max( dp[ j ] ) + cnt[ i ]   且 i | j

然后用扩展欧几里德求出走到下一步需要乘多少。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long using namespace std; const int N = 2e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); LL n, m, dp[N], path[N];
bool ban[N];
vector<LL> gg[N];
vector<LL> ans;
vector<LL> vc; LL x, y; LL exgcd(LL a, LL b, LL &x, LL &y) {
if(!b) {
x = ; y = ;
return a;
} else {
LL gcd, t; gcd = exgcd(b, a % b, x, y);
t = x; x = y; y = t - (a / b) * y;
return gcd;
}
} LL dfs(LL x) {
if(~dp[x]) return dp[x];
dp[x] = ;
for(int i = * x; i < m; i += x) {
LL cnt = dfs(i);
if(cnt > dp[x]) {
dp[x] = cnt;
path[x] = i;
}
}
dp[x] += SZ(gg[x]);
return dp[x];
} LL solve(LL pre, LL to) {
if(pre == -) ans.push_back(to);
else {
LL gcd = exgcd(pre, -m, x, y);
if(to % gcd) exit();
x *= to / gcd; y *= to / gcd;
LL mo = abs(m / gcd);
x = ((x % mo) + mo) % mo;
ans.push_back(x);
}
return to;
} int main() {
memset(dp, -, sizeof(dp));
scanf("%lld%lld", &n, &m);
for(int i = ; i <= n; i++) {
int x; scanf("%d", &x);
ban[x] = true;
}
for(LL i = ; i < m; i++) {
if(ban[i]) continue;
gg[__gcd(i, m)].push_back(i);
}
dfs();
LL pre = -;
for(LL gcd = ; gcd; gcd = path[gcd]) {
for(int i = ; i < SZ(gg[gcd]); i++)
pre = solve(pre, gg[gcd][i]);
}
if(!ban[]) ans.push_back();
printf("%d\n", SZ(ans));
for(auto& t : ans) printf("%lld ", t);
puts("");
return ;
} /*
*/

最新文章

  1. Sybase 常用SQL
  2. aspx后台传递Json到前台的两种接收方法
  3. [Android教程]EditText怎样限制用户的输入?数字/字母/邮箱
  4. Netbeans快捷键
  5. robot API笔记6
  6. Core Text概述
  7. cannot restore segment prot after reloc: Permission denied
  8. 国标电表DLT645转MODBUS TCP协议转换器MRD-5021,工业设备,浪涌三级保护MRD
  9. CSDN开源夏令营 百度数据可视化实践 ECharts(8)问题分析
  10. H5输入框实时记录文字个数
  11. jquery.tagsinput的使用例子,包括模糊查询后端代码
  12. Linq 生成运算符 Empty,Range,Repeat
  13. 使用mediainfo工具统计每个视频文件(媒体文件)播放时长
  14. mysql 开发基础系列19 触发器
  15. python类型错误:&#39;NoneType&#39; object is not subscriptable
  16. bind this指针
  17. SQL左右连接中的on and和on where的区别
  18. SPSS-判别分析
  19. Java 集合、Iterator迭代器、泛型等
  20. hbase使用MapReduce操作3(实现将 fruit 表中的一部分数据,通过 MR 迁入到 fruit_mr 表中)

热门文章

  1. 【题解】 bzoj4033: [HAOI2015]树上染色* (动态规划)
  2. 学习Spring Boot:(十三)配置 Shiro 权限认证
  3. 【poj2187】最远点对(勉强凑数)
  4. debian8.4 ibus中文输入法
  5. HDU 3537 基础翻硬币模型 Mock Turtles 向NIM转化
  6. Windows系统安装————windows7 企业版 无法安装 NET.framework4.52-4.6版本在WIN7下解决办法
  7. 微信公众号用户OpenID同步导出系统
  8. CSS的力量:用一个DIV画图
  9. [转]Linux联网问题
  10. 日常训练赛 Problem C – Complete Naebbirac’s sequence