计蒜客-2017 计蒜之道 复赛-F-腾讯消消乐
来源:互联网 发布:js将时间戳格式化 编辑:程序博客网 时间:2024/04/29 22:35
ACM模版
描述
题解
典型的状压 dp,因为最多 18 个,所以状态是有限的,也就
代码
#include <iostream>#include <cstdio>#include <vector>#include <cstring>using namespace std;typedef long long ll;const ll MAXN = 18;const ll MOD = 1e9 + 7;ll A[MAXN + 10];ll dp[2][(1 << MAXN) + 10];inline void mod(ll &x){ x -= x / MOD * MOD;}inline ll gcd(ll a, ll b){ return b == 0 ? a : gcd(b, a % b);}vector<ll> vl;int main(){ ll n, k; cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> A[i]; } int cnt = 0, w; ll a; for (int i = 1; i <= n; i++) { a = A[i]; w = 0; for (int j = i; j <= n; j++) { if ((a = gcd(a, A[j])) >= k) { w ^= 1 << (n - j); dp[cnt][w] = 1; } else { break; } } } cnt++; ll t, j_, sz, ans = 0; for (int i = 2; i <= n; i++, cnt ^= 1) { memset(dp[cnt], 0, sizeof(dp[cnt])); ans += (i - 1) * dp[cnt ^ 1][(1 << n) - 1]; ans %= MOD; for (int j = 0; j < (1 << n); j++) {// cout << dp[cnt^1][j] << ' '; if (dp[cnt ^ 1][j] == 0) { continue; } t = n; j_ = j; vl.clear(); while (t) { if (!(j_ & 1)) { vl.push_back(t); } j_ >>= 1; t--; } sz = vl.size(); for (int i_ = 0; i_ < sz; i_++) { a = A[vl[i_]]; w = 0; for (int j_ = i_; j_ < sz; j_++) { if ((a = gcd(a, A[vl[j_]])) >= k) { w ^= 1 << (n - vl[j_]); dp[cnt][j ^ w] += dp[cnt ^ 1][j]; dp[cnt][j ^ w] %= MOD; } else { break; } } } }// putchar(10); } ans += n * dp[cnt ^ 1][(1 << n) - 1]; ans %= MOD; cout << ans << endl; return 0; }
阅读全文
0 0
- 计蒜客-2017 计蒜之道 复赛-F-腾讯消消乐
- 2017 计蒜之道 复赛 腾讯消消乐
- 计蒜之道复赛 腾讯消消乐
- 2017 计蒜之道 复赛 腾讯消消乐【状压dp】
- 计蒜之道-2017复赛-腾讯消消乐(状压DP)
- 计蒜之道-2017复赛-腾讯消消乐(状压DP)
- 计蒜之道2017F-腾讯消消乐(状压dp)
- 计蒜客 2017 复赛 腾讯消消乐 状压dp
- 计蒜客 2017 复赛 腾讯消消乐 (状压dp)
- 2017计蒜之道 复赛 BDF
- 2017 计蒜之道 复赛 题解
- 计蒜客-2017 计蒜之道 复赛-B-Windows 画图
- 计蒜之道复赛F题 菜鸟物流的运输网络(网络流)
- 计蒜之道 2016 复赛 F.菜鸟物流的运输网络
- 2017计蒜之道复赛-Windows 画图
- 2017计蒜之道程序设计大赛复赛题解
- 2017 计蒜之道 复赛 <状压DP+最短路>
- [反演] 2017 计蒜之道 复赛 A. 阿里云秘钥池
- Google-Guice入门介绍
- 1、ES6 === let && const
- saltstack安装及基本命令
- openCV基础函数imread第二个参数
- 电子阅读器模块
- 计蒜客-2017 计蒜之道 复赛-F-腾讯消消乐
- 复杂条件查询
- 权限管理模块
- 顺序表、结构体、深拷贝、重载运算符,快速排序的综合运用
- 在线电影订票系统
- KMP算法思考
- 网页浏览器模块
- IDEA下Spring项目报错-could not autowire. no beans of“” type found
- 企业内部邮件管理