Codeforces839D Winter is here 容斥
/**
题目:Codeforces839D Winter is here
链接:http://codeforces.com/contest/839/problem/D
题意:给定n个数,求所有的最大公约数>1的子集的贡献之和。
一个子集的贡献为最大公约数*子集大小
思路:
cnt[i]表示约数为i的数的个数。
ans[i]表示约数为i的所有k的和。 已知ans[i]可以求得贡献为i*ans[i]; cnt[i]个数的长度为(sigma(k))cnt[i]*(2^(cnt[i]-1)); 即所有子集长度的和。
但是其中有些子集的gcd不是等于i。所以要减去。
逆序枚举,ans[i] = cnt[i]*(2^(cnt[i]-1)) - sigma(ans[j]);(j%i==0&&j/i>1) */
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include <iostream>
#include <vector>
using namespace std;
typedef long long LL;
#define ms(x,y) memset(x,y,sizeof x)
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + ;
const int maxn = 1e6 + ;
int cnt[maxn];
int sum[maxn];
LL ans[maxn];
LL Pow(LL x, int y)
{
LL p = ;
while (y)
{
if (y & ) p = p*x%mod;
x = x*x%mod;
y >>= ;
}
return p;
}
int main()
{
int T, cas = ;
int n;
while (scanf("%d",&n)==)
{
int x, mas = ;
ms(cnt,);
ms(sum,);
ms(ans,);
for(int i = ; i <= n; i++){
scanf("%d",&x);
mas = max(mas,x);
sum[x]++;
}
for(int i = ; i < maxn; i++){
for(int j = i; j < maxn; j+=i){
cnt[i] += sum[j];
}
}
LL res = ;
for(int i = mas; i >= ; i--){
if(cnt[i]==) continue;
LL num = ;
for(int j = *i; j <= mas; j+=i){
num = (num+ans[j])%mod;
}
ans[i] = (cnt[i]*Pow(,cnt[i]-)%mod-num+mod)%mod;
res = (res+ans[i]*i%mod)%mod;
}
printf("%I64d\n",res);
} return ;
}
Codeforces839D Winter is here 容斥的更多相关文章
- Codeforces Round #428 (Div. 2) D. Winter is here 容斥
D. Winter is here 题目连接: http://codeforces.com/contest/839/problem/D Description Winter is here at th ...
- POJ1091跳蚤(容斥 + 唯一分解 + 快速幂)
题意:规定每次跳的单位 a1, a2, a3 …… , an, M,次数可以为b1, b2, b3 …… bn, bn + 1, 正好表示往左,负号表示往右, 求能否调到左边一位,即 a1* b1 ...
- HDU 4059 容斥初步练习
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> ...
- HDU 4336 Card Collector (期望DP+状态压缩 或者 状态压缩+容斥)
题意:有N(1<=N<=20)张卡片,每包中含有这些卡片的概率,每包至多一张卡片,可能没有卡片.求需要买多少包才能拿到所以的N张卡片,求次数的期望. 析:期望DP,是很容易看出来的,然后由 ...
- 【BZOJ-4455】小星星 容斥 + 树形DP
4455: [Zjoi2016]小星星 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 204 Solved: 137[Submit][Status] ...
- cf#305 Mike and Foam(容斥)
C. Mike and Foam time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- UVa12633 Super Rooks on Chessboard(容斥 + FFT)
题目 Source http://acm.hust.edu.cn/vjudge/problem/42145 Description Let’s assume there is a new chess ...
- PE-1 & 暴模|容斥
题意: 求1000以下3或5的倍数之和. SOL: 暴模也是兹瓷的啊... 那么就想到了初赛悲催的滚粗...容斥忘了加上多减的数了... 然后对着题...T = 3*333*(1+333)/2 + 5 ...
- HDU 5838 (状压DP+容斥)
Problem Mountain 题目大意 给定一张n*m的地图,由 . 和 X 组成.要求给每个点一个1~n*m的数字(每个点不同),使得编号为X的点小于其周围的点,编号为.的点至少大于一个其周围的 ...
随机推荐
- Spring框架学习(8)spring mvc上传下载
内容源自:spring mvc上传下载 如下示例: 页面: web.xml: <?xml version="1.0" encoding="UTF-8"?& ...
- 修改Jenkins的BUILD_NUMBER
因为特殊原因需要修改jenkins的BUILD_NUMBER环境变量,现在将修改方法记录一下, Jenkins ->系统管理->脚本命令行 输入以下脚本,运行就Ok Jenkins.ins ...
- php求斐波那契数列
<?php function feibonaqi(){ //参数$num表示为第$num个数之前的所有斐波那契数列 $arr = array(); //定义一个空变量用来存放斐波那契数列的数组 ...
- 001-使用idea开发环境安装部署,npm工具栏,脚本运行
一.概述 参看官方文档:https://ant.design/docs/spec/introduce-cn 其中包含了设计价值观.设计原则.视觉.模式.可视化.动态等. 其中Ant Design 的 ...
- jQuery-mobile 学习笔记之三(事件监听)
续上 触摸事件 - 当用户触摸屏幕时触发(敲击和滑动) 滚动事件 - 当上下滚动时触发 方向事件 - 当设备垂直或水平旋转时触发 页面事件 - 当页面被显示.隐藏.创建.载入以及/或卸载时触发 一.初 ...
- ubuntu使用du命令查看一级子目录存储空间大小
命令如下: ls | xargs du -ksh 可以ls不同的目录以查看不同的目录下的一级子目录大小.直接使用ls为当前目录下的一级子目录大小. 查看其他目录的大小: ls -d dirname/* ...
- linux中的strip命令简介------给文件脱衣服
1.去掉-g,等于程序做了--strip-debug2.strip程序,等于程序做了--strip-debug和--strip-symbol 作为一名Linux开发人员, 如果没有听说过strip命令 ...
- Layui 弹出层组件——layer的模块化开发实例应用
Layui 弹出层组件——layer的模块化开发实例应用 1.首先在package.json中引入layer组件依赖 2.在源码中应用这个依赖 3.在源码中编写代码应用此组件 4.效果验证:点击日历上 ...
- 【SpringMVC学习01】宏观上把握SpringMVC框架
springmvc是一个基于mvc的web框架,是spring框架的一个模块,所以springmvc和spring无需通过中间整合层进行整合.我们先来看下spring的一个架构模型,看springmv ...
- PL/SQL 美化器不能解析文本
1.问题:PL/SQL美化器不能解析文本 原始sql语句如下: CREATE OR REPLACE VIEW V_GGXZBM AS SELECT XZBM,XZMC,CASE WHEN PARENT ...