zoj[3868]gcd期望
题意:求n个数组成的集合的所有非空子集的gcd的期望
大致思路:对于一个数x,设以x为约数的数的个数为cnt[x],所组成的非空集合个数有2^cnt[x]-1个,这其中有一些集合的gcd是x的倍数的,怎么求得最终结果呢?下面来说明过程。
令f[x] = 2^cnt[x]-1,表示以x为gcd的集合个数。令maxn为所有数的最大值,一开始f[maxn]=2^cnt[maxn]-1是肯定正确的。若从大到小更新f数组,类似数学归纳法,f[x]需要减去f[2x]、f[3x]、...、f[px],px<=maxn,而f[2x]、f[3x]、...、f[px]都是正确的,所以f[x]也是正确的。所以可以得到正确的f数组,有了f数组,答案自然出来了。
1 #pragma comment(linker, "/STACK:10240000,10240000") 2 3 #include <iostream> 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstdlib> 7 #include <cstring> 8 #include <map> 9 #include <queue> 10 #include <deque> 11 #include <cmath> 12 #include <vector> 13 #include <ctime> 14 #include <cctype> 15 #include <set> 16 #include <bitset> 17 #include <functional> 18 #include <numeric> 19 #include <stdexcept> 20 #include <utility> 21 22 using namespace std; 23 24 #define mem0(a) memset(a, 0, sizeof(a)) 25 #define lson l, m, rt << 1 26 #define rson m + 1, r, rt << 1 | 1 27 #define define_m int m = (l + r) >> 1 28 #define rep_up0(a, b) for (int a = 0; a < (b); a++) 29 #define rep_up1(a, b) for (int a = 1; a <= (b); a++) 30 #define rep_down0(a, b) for (int a = b - 1; a >= 0; a--) 31 #define rep_down1(a, b) for (int a = b; a > 0; a--) 32 #define all(a) (a).begin(), (a).end() 33 #define lowbit(x) ((x) & (-(x))) 34 #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {} 35 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {} 36 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {} 37 #define pchr(a) putchar(a) 38 #define pstr(a) printf("%s", a) 39 #define sstr(a) scanf("%s", a); 40 #define sint(a) ReadInt(a) 41 #define sint2(a, b) ReadInt(a);ReadInt(b) 42 #define sint3(a, b, c) ReadInt(a);ReadInt(b);ReadInt(c) 43 #define pint(a) WriteInt(a) 44 #define if_else(a, b, c) if (a) { b; } else { c; } 45 #define if_than(a, b) if (a) { b; } 46 #define test_pint1(a) printf("var1 = %d\n", a) 47 #define test_pint2(a, b) printf("var1 = %d, var2 = %d\n", a, b) 48 #define test_pint3(a, b, c) printf("var1 = %d, var2 = %d, var3 = %d\n", a, b, c) 49 50 typedef double db; 51 typedef long long LL; 52 typedef pair<int, int> pii; 53 typedef multiset<int> msi; 54 typedef set<int> si; 55 typedef vector<int> vi; 56 typedef map<int, int> mii; 57 58 const int dx[8] = {0, 0, -1, 1}; 59 const int dy[8] = {-1, 1, 0, 0}; 60 const int maxn = 1e6 + 7; 61 const int maxm = 1e5 + 7; 62 const int maxv = 1e7 + 7; 63 const int max_val = 1e6 + 7; 64 const int MD = 998244353; 65 const int INF = 1e9 + 7; 66 const double pi = acos(-1.0); 67 const double eps = 1e-10; 68 69 template<class T>T gcd(T a, T b){return b==0?a:gcd(b,a%b);} 70 template<class T>void ReadInt(T &x){char c=getchar();while(!isdigit(c))c=getchar();x=0;while(isdigit(c)){x=x*10+c-'0';c=getchar();}} 71 template<class T>void WriteInt(T i) {int p=0;static int b[20];if(i == 0) b[p++] = 0;else while(i){b[p++]=i%10;i/=10;}for(int j=p-1;j>=0;j--)pchr('0'+b[j]);} 72 template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;} 73 template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;} 74 template<class T>T condition(bool f, T a, T b){return f?a:b;} 75 template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];} 76 int make_id(int x, int y, int n) { return x * n + y; } 77 78 int pow_mod(int a, int b) { 79 static int buf[100]; 80 int p = 0; 81 while (b) { 82 buf[p++] = b & 1; 83 b >>= 1; 84 } 85 LL ans = 1; 86 rep_down0(i, p) { 87 ans = ans * ans % MD; 88 if (buf[i]) ans = ans * a % MD; 89 } 90 return ans; 91 } 92 93 int cnt[maxn], c[maxn], f[maxn]; 94 95 int main() { 96 //freopen("in.txt", "r", stdin); 97 //freopen("out.txt", "w", stdout); 98 int T; 99 cin >> T; 100 while (T--) { 101 mem0(cnt); 102 mem0(c); 103 int n, k; 104 cin >> n >> k; 105 int max_n = 0; 106 rep_up0(i, n) { 107 int x; 108 sint(x); 109 cnt[x]++; 110 max_update(max_n, x); 111 } 112 rep_up1(i, max_n) { 113 for (int j = i; j <= max_n; j += i) { 114 c[i] += cnt[j]; 115 } 116 } 117 rep_up1(i, max_n) f[i] = (pow_mod(2, c[i]) + MD - 1) % MD; 118 LL ans = 0; 119 rep_down1(i, max_n) { 120 for (int j = 2 * i; j <= max_n; j += i) { 121 f[i] = (f[i] - f[j] + MD) % MD; 122 } 123 ans = (ans + (LL)f[i] * (pow_mod(i, k))) % MD; 124 } 125 cout << ans << endl; 126 } 127 return 0; 128 }
View Code
转载于:https://www.cnblogs.com/jklongint/p/4424901.html
zoj[3868]gcd期望相关推荐
- GCD Expectation ZOJ - 3868 (容斥)
Edward has a set of n integers {a1, a2,-,an}. He randomly picks a nonempty subset {x1, x2,-,xm} (eac ...
- ZOJ.3551.Bloodsucker(期望DP)
题目链接 \(Description\) 有1个吸血鬼和n-1个人,每天有且只会有两个人/吸血鬼相遇,如果是人与吸血鬼相遇,那个人会有p的概率变成吸血鬼:否则什么也不发生.求n个都变成吸血鬼的期望天数 ...
- 浅析欧几里德算法 GCD和LCM
前言 欧几里德算法作为有着非常简短的实现的算法,可能很多初学者(包括当时的我)都不求甚解.本文给出了GCD.LCM的性质,以及欧几里德算法的实现.证明和时间复杂度推导. 这里是我的个人网站: http ...
- 期望+DP ZOJ 3929 Deque and Balls
题目链接 题意:给你n个数,按照顺序依次放入一个双端队列(可放在头部,也可以放在尾部),求xi > xi+1的期望 * 2^n mod (1e9 +7) 分析:期望*2^n=出现这种排法的概率* ...
- 【ZOJ - 3329】One Person Game(带循环的概率dp,数学期望,高斯消元,数学)
题干: There is a very simple and interesting one-person game. You have 3 dice, namely Die1, Die2 and D ...
- 【ZOJ - 2949】Coins of Luck (概率dp,期望)
题干: 给你两种泡面 各N包,然后你扔硬币,正面朝上吃第一种,反面呢 吃第二种,有任意一种吃完时 就不需要抛硬币了,求停止抛硬币的期望. Sample Input 2 1 2 Sample Outpu ...
- poj 2096 , zoj 3329 , hdu 4035 —— 期望DP
题目:http://poj.org/problem?id=2096 题目好长...意思就是每次出现 x 和 y,问期望几次 x 集齐 n 种,y 集齐 s 种: 所以设 f[i][j] 表示已经有几种 ...
- 牛客 16138 愤怒的巨巨(期望,gcd)
链接:https://ac.nowcoder.com/acm/problem/16138 来源:牛客网 题目描述 在511没人敢惹盼成巨巨,因为盼成巨巨是我们511的学神! 周末,巨巨让乙超大佬去买一 ...
- CF912D Fishes 期望
题意翻译 Description 有一个长为nnn ,宽为mmm 的鱼缸,还有一个边长为rrr 的正方形渔网.你可以往鱼缸里放kkk 条鱼,问用渔网随机在浴缸里捞鱼的最大期望是多少.不懂什么是期望的自 ...
- ZOJ 3597 Hit the Target! (线段树扫描线 -- 矩形所能覆盖的最多的点数)
ZOJ 3597 题意是说有n把枪,有m个靶子,每把枪只有一发子弹(也就是说一把枪最多只能打一个靶子), 告诉你第 i 把枪可以打到第j个靶, 现在等概率的出现一个连续的P把枪,在知道这P把枪之后,你 ...
最新文章
- 微服务治理实践:服务查询
- 我用Python爬取英雄联盟的皮肤,隔壁家的小弟弟都馋哭了
- python爬虫下载-用Python爬虫下载整本小说
- codesys 简单案例_第一章:初识Codesys-1.4从一个示例程序讲起
- [高效时间管理]复盘篇
- 访谈编码怎么做_怎么才能让口才得到提升
- 漫步数学分析二十一——逐点收敛与一致收敛
- 安卓学习笔记24:常用控件 - 循环器视图
- Intel 64/x86_64/IA-32/x86处理器 - 指令格式(8) - 80386/32位指令前缀
- 01-windows下python爬取网页上的图片
- Linux下安装ntp时间同步服务器
- Keyphrase Extraction 一个快速从中文里抽取关键短语的工具
- python处理多个excel数据_python 数据分析基础 day8-pandas读写多个excel文件
- 打造了一把安全的锁,不料把自己也锁在了里面
- 归并排序法计算逆序对数
- linux grep查找指定文件或目录下文件的字符
- php手机靓号选号系统源码_最好的手机靓号网站源码-鹏博靓号系统
- python turtle库下载_python3中安装turtle库
- handlersocket php,handlersocket安装配置
- 一文了解推挽输出结构Output_push_pull
热门文章
- java常见异常_译文最常见的10种Java异常问题
- Linux系统编程 -- 进程 信号
- nginx master-worker进程间通信
- Linux网络编程——端口复用(多个套接字绑定同一个端口----避免服务器重启时,端口绑定不上)
- Linux 网络编程 —— TCP编程之客户端 向服务器发送数据 接收服务器发来的数据
- redis内核单元测试框架
- 4.3.2深度定时任务(TimerTask in Depth)
- jQuery操作CSS属性的相关方法
- 【5分钟 Paper】Deep Reinforcement Learning with Double Q-learning
- [转]SQL2008关于c001f011的错误解决办法