cf 645F Cowslip Collections 组合数学 + 简单数论
8 seconds
512 megabytes
standard input
standard output
In an attempt to make peace with the Mischievious Mess Makers, Bessie and Farmer John are planning to plant some flower gardens to complement the lush, grassy fields of Bovinia. As any good horticulturist knows, each garden they plant must have the exact same arrangement of flowers. Initially, Farmer John has n different species of flowers he can plant, with ai flowers of the i-th species.
On each of the next q days, Farmer John will receive a batch of flowers of a new species. On day j, he will receive cj flowers of the same species, but of a different species from those Farmer John already has.
Farmer John, knowing the right balance between extravagance and minimalism, wants exactly k species of flowers to be used. Furthermore, to reduce waste, each flower of the k species Farmer John chooses must be planted in some garden. And each of the gardens must be identical; that is to say that each of the k chosen species should have an equal number of flowers in each garden. As Farmer John is a proponent of national equality, he would like to create the greatest number of gardens possible.
After receiving flowers on each of these q days, Farmer John would like to know the sum, over all possible choices of k species, of the maximum number of gardens he could create. Since this could be a large number, you should output your result modulo 109 + 7.
The first line of the input contains three integers n, k and q (1 ≤ k ≤ n ≤ 100 000, 1 ≤ q ≤ 100 000).
The i-th (1 ≤ i ≤ n) of the next n lines of the input contains an integer ai (1 ≤ ai ≤ 1 000 000), the number of flowers of species i Farmer John has initially.
The j-th (1 ≤ j ≤ q) of the next q lines of the input contains an integer cj (1 ≤ cj ≤ 1 000 000), the number of flowers of a new species Farmer John receives on day j.
After each of the q days, output the sum of the maximum possible number of gardens, where the sum is taken over all possible choices of k species, modulo 109 + 7.
3 3 2
4
6
9
8
6
5
16
4 1 2
6
5
4
3
2
1
20
21
In the first sample case, after the first day Farmer John has (4, 6, 9, 8) of each type of flower, and k = 3.
Choosing (4, 6, 8) lets him make 2 gardens, each with (2, 3, 4) of each flower, respectively. Choosing (4, 6, 9), (4, 9, 8) and (6, 9, 8) each only let him make one garden, since there is no number of gardens that each species can be evenly split into. So the sum over all choices of k = 3 flowers is 2 + 1 + 1 + 1 = 5.
After the second day, Farmer John has (4, 6, 9, 8, 6) of each flower. The sum over all choices is 1 + 2 + 2 + 1 + 1 + 2 + 2 + 3 + 1 + 1 = 16.
In the second sample case, k = 1. With x flowers Farmer John can make x gardens. So the answers to the queries are 6 + 5 + 4 + 3 + 2 = 20 and 6 + 5 + 4 + 3 + 2 + 1 = 21.
题意:
n,k,q <= 10^5,ai <= 10^6
一个数组,初始长度为n,元素为a1~an
接下来有q天,每一天会向数组里面新加入一个数,第i天元素个数有n + i个
数组取k个元素有C(n + i , k )种方案,求每一个种方案的k的数的gcd之和
思路:
这道题目明显具有阶段性,向其中加入一个数cur后,求解ans不用重新计算一遍,只需要考虑cur取的情况,再从之前的数中取k - 1 个数(1),求gcd之和,再累加上以前的答案即可
对于(1)所有方案,gcd一定是cur的约数,所以我们考虑枚举cur的约数
用一个数组f[i]表示cur之前的数中,是i的倍数的数的个数
对于约数x
ans += C(f[x],k - 1) * x,但是此时把一些gcd是x的倍数的情况也算进去了
反过来说,对于x,我们要把x的约数多算的部分给退回去,差不多就是容斥那样
推一下公式,定义
g[i] = i - sigma(g[d],d < i && d | i)
这样相当于是给每一个数一个权值,这样就不用再去考虑重复计算的情况了
这样对于cur的约数x
ans += C(f[x],k - 1) * g[x]
复杂度O(sqrt(10^6)) = O(10^3)
总复杂度 = O(10 ^ 9 + 10 ^ 3 * 10 * 5) = O(10 ^ 9 + 10 ^ 8)
1.预处理:
jie[i] = i!
comb[i] = C(i,k - 1)
g[i] = i - sigma(g[d],d < i && d | i) (1 <= i <= 10 ^ 6,d是i的约数且 != i)
(预处理g的复杂度=10 ^ 6 * 10 ^ 3 = 10 ^ 9)
//File Name: cf645F.cpp
//Author: long
//Mail: 736726758@qq.com
//Created Time: 2016年05月15日 星期日 19时58分07秒 #include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <vector>
#include <math.h> #define LL long long
#define pb push_back using namespace std; const int MAXN = + ;
const int MAXM = + ;
const int MOD = (int)1e9 + ; LL jie[MAXN << ];
LL comb[MAXN << ];
LL ans;
int g[MAXM];
int f[MAXM];
vector<int> di; void get_di(int x){
di.clear();
int ma = (int)sqrt(x + 0.5);
for(int i=,j;i<=ma;i++){
if(x % i == ){
di.pb(i);
j = x / i;
if(j != i)
di.pb(j);
}
}
} LL qp(LL x){
LL res = ,y = MOD - ;
while(y){
if(y & ) res = res * x % MOD;
x = x * x % MOD;
y >>= ;
}
return res;
} void init(int n,int k){
ans = ;
jie[] = ;
for(int i=;i<=n;i++){
jie[i] = jie[i - ] * i % MOD;
}
for(int i=;i<=n;i++){
if(i < k) comb[i] = ;
else comb[i] = jie[i] * qp(jie[k] * jie[i - k] % MOD) % MOD;
}
for(int i=,ma;i<=MAXM;i++){
g[i] = i;
get_di(i);
ma = di.size();
for(int j=;j<ma;j++){
if(di[j] == i) continue;
g[i] -= g[di[j]];
}
}
} void query(int x){
get_di(x);
int ma = di.size();
for(int i=;i<ma;i++){
(ans += comb[f[di[i]]++] * g[di[i]] % MOD ) %= MOD;
}
} void solve(int n,int k,int q){
init(n + q,k - );
for(int i=,a;i<=n;i++){
scanf("%d",&a);
query(a);
}
for(int i=,a;i<=q;i++){
scanf("%d",&a);
query(a);
printf("%d\n",ans);
}
} int main(){
int n,k,q;
scanf("%d %d %d",&n,&k,&q);
solve(n,k,q);
return ;
}
cf 645F Cowslip Collections 组合数学 + 简单数论的更多相关文章
- CROC 2016 - Elimination Round (Rated Unofficial Edition) F - Cowslip Collections 数论 + 容斥
F - Cowslip Collections http://codeforces.com/blog/entry/43868 这个题解讲的很好... #include<bits/stdc++.h ...
- (step7.2.1)hdu 1395(2^x mod n = 1——简单数论)
题目大意:输入一个整数n,输出使2^x mod n = 1成立的最小值K 解题思路:简单数论 1)n可能不能为偶数.因为偶数可不可能模上偶数以后==1. 2)n肯定不可能为1 .因为任何数模上1 == ...
- 简单数论之整除&质因数分解&唯一分解定理
[整除] 若a被b整除,即a是b的倍数,那么记作b|a("|"是整除符号),读作"b整除a"或"a能被b整除".b叫做a的约数(或因数),a ...
- 2018.12.17 bzoj1406 : [AHOI2007]密码箱(简单数论)
传送门 简单数论暴力题. 题目简述:要求求出所有满足x2≡1mod  nx^2\equiv1 \mod nx2≡1modn且0≤x<n0\ ...
- CF 984C Finite or not? (数论)
CF 984C Finite or not? (数论) 给定T(T<=1e5)组数据,每组数据给出十进制表示下的整数p,q,b,求问p/q在b进制意义下是否是有限小数. 首先我们先把p/q约分一 ...
- Pairs Forming LCM (LightOJ - 1236)【简单数论】【质因数分解】【算术基本定理】(未完成)
Pairs Forming LCM (LightOJ - 1236)[简单数论][质因数分解][算术基本定理](未完成) 标签: 入门讲座题解 数论 题目描述 Find the result of t ...
- Help Hanzo (LightOJ - 1197) 【简单数论】【筛区间质数】
Help Hanzo (LightOJ - 1197) [简单数论][筛区间质数] 标签: 入门讲座题解 数论 题目描述 Amakusa, the evil spiritual leader has ...
- Aladdin and the Flying Carpet (LightOJ - 1341)【简单数论】【算术基本定理】【分解质因数】
Aladdin and the Flying Carpet (LightOJ - 1341)[简单数论][算术基本定理][分解质因数](未完成) 标签:入门讲座题解 数论 题目描述 It's said ...
- Sigma Function (LightOJ - 1336)【简单数论】【算术基本定理】【思维】
Sigma Function (LightOJ - 1336)[简单数论][算术基本定理][思维] 标签: 入门讲座题解 数论 题目描述 Sigma function is an interestin ...
随机推荐
- kuangbin_ShortPath J (POJ 1511)
其实虽然一开始有被这个题的8000MS 和 256MB限制又被吓到 但是严格来说跟之前的POJ 3268是一样的做法只是数据大了点 但是问题就出在数据大了点上 其实严格来说也不大 1e6 数组加起来大 ...
- ubuntu下nfs服务器的安装与配置
nfs服务器的安装和配置 1.安装nfs 服务器,前提是你的系统能连上网. 2.设置/etc/exports配置文件 (1) 进入/etc/exports配置文件 (2) 在最后一行加入红色那行,/h ...
- Oracle DBWR,LGWR,CKPT,ARCH 触发条件 总结
一. DBWR写磁盘数据触发条件 1. 当进程在辅助LRU链表和主LRU链表上扫描以查找可以覆盖的buffer header[空闲缓冲区]时,如果已经扫描的buffer header的数量到达一定的 ...
- ABBYY将JPEG文件转换成Word文档的方法
日常工作中处理JPEG格式的图像文件时,有时需要转换成Word文档进行编辑,市场上应用而生了很多转换工具,相信不少人听说过OCR(光学字符识别)软件,可以用来转换图像文件,而在OCR软件中, ABBY ...
- MongoDB权限管理之用户名和密码的操作
MongoDB默认是不需要输入用户名和密码,客户就可以登录的.但是出于安全性的考虑,我们还是要为其设置用户名和密码.本文主要介绍的是MongoDB权限管理之用户名和密码的操作,希望能对您有所帮助. 本 ...
- 不透明度(兼容IE8,chrome,firefox)
background-color: rgba(0, 0, 0, 0.2); background-color: black; opacity: 0.2; filter: Alpha(opacity=2 ...
- asp.net中virtual和abstract的区别
这篇文章主要介绍了asp.net中virtual和abstract的区别,较为详细的分析了virtual与abstract的概念与具体用法,并以实例的形式予以总结归纳,需要的朋友可以参考下 本文实例分 ...
- 【Reporting Services 报表开发】— 数据表存储格式修改
文本框 Format属性:日期:输入d(表示简易日期).2007/5/1 0:00:00 输入d之后 变成 2007/5/1 金额:输入C0(表示货币),系统会根据设定值产生对应的货币符号,至于0 ...
- java 常用集合例子
package test; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import ...
- SQL Server 2005 分区表创建实例
--创建一个分区函数(默认为左边界)CREATE PARTITION FUNCTION PARTFUNC1(INT)AS RANGEFOR VALUES(1000,2000,3000,4000,500 ...