zoukankan      html  css  js  c++  java
  • cf 645F Cowslip Collections 组合数学 + 简单数论

    http://codeforces.com/contest/645/problem/F
    F. Cowslip Collections
    time limit per test
    8 seconds
    memory limit per test
    512 megabytes
    input
    standard input
    output
    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.

    Input

    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.

    Output

    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.

    Examples
    Input
    3 3 2
    4
    6
    9
    8
    6
    Output
    5
    16
    Input
    4 1 2
    6
    5
    4
    3
    2
    1
    Output
    20
    21
    Note

    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)

      1                                             
      2   //File Name: cf645F.cpp
      3   //Author: long
      4   //Mail: 736726758@qq.com
      5   //Created Time: 2016年05月15日 星期日 19时58分07秒
      6                                    
      7 
      8 #include <stdio.h>
      9 #include <algorithm>
     10 #include <iostream>
     11 #include <string.h>
     12 #include <vector>
     13 #include <math.h>
     14 
     15 #define LL long long
     16 #define pb push_back
     17 
     18 using namespace std;
     19 
     20 const int MAXN = 100000 + 3;
     21 const int MAXM = 1000000 + 3;
     22 const int MOD = (int)1e9 + 7;
     23 
     24 LL jie[MAXN << 1];
     25 LL comb[MAXN << 1];
     26 LL ans;
     27 int g[MAXM];
     28 int f[MAXM];
     29 vector<int> di;
     30 
     31 void get_di(int x){
     32     di.clear();
     33     int ma = (int)sqrt(x + 0.5);
     34     for(int i=1,j;i<=ma;i++){
     35         if(x % i == 0){
     36             di.pb(i);
     37             j = x / i;
     38             if(j != i)
     39                 di.pb(j);
     40         }
     41     }
     42 }
     43 
     44 LL qp(LL x){
     45     LL res = 1,y = MOD - 2;
     46     while(y){
     47         if(y & 1) res = res * x % MOD;
     48         x = x * x % MOD;
     49         y >>= 1;
     50     }
     51     return res;
     52 }
     53 
     54 void init(int n,int k){
     55     ans = 0;
     56     jie[0] = 1;
     57     for(int i=1;i<=n;i++){
     58         jie[i] = jie[i - 1] * i % MOD;
     59     }
     60     for(int i=0;i<=n;i++){
     61         if(i < k) comb[i] = 0;
     62         else comb[i] = jie[i] * qp(jie[k] * jie[i - k] % MOD) % MOD;
     63     }
     64     for(int i=1,ma;i<=MAXM;i++){
     65         g[i] = i;
     66         get_di(i);
     67         ma = di.size();
     68         for(int j=0;j<ma;j++){
     69             if(di[j] == i) continue;
     70             g[i] -= g[di[j]];
     71         }
     72     }
     73 }
     74 
     75 void query(int x){
     76     get_di(x);
     77     int ma = di.size();
     78     for(int i=0;i<ma;i++){
     79         (ans += comb[f[di[i]]++] * g[di[i]] % MOD ) %= MOD;
     80     }
     81 }
     82 
     83 void solve(int n,int k,int q){
     84     init(n + q,k - 1);
     85     for(int i=1,a;i<=n;i++){
     86         scanf("%d",&a);
     87         query(a);
     88     }
     89     for(int i=1,a;i<=q;i++){
     90         scanf("%d",&a);
     91         query(a);
     92         printf("%d
    ",ans);
     93     }
     94 }
     95 
     96 int main(){
     97     int n,k,q;
     98     scanf("%d %d %d",&n,&k,&q);
     99     solve(n,k,q);
    100     return 0;
    101 }
    View Code

     

  • 相关阅读:
    ZJOI2018]历史 做题心得 Flandre
    Codeforces 1495F 搞了一上午的心得 Flandre
    关于JS的跨域通信的几种解决方案 (转)
    我对php框架的理解和看法
    ob_get_contents();ob_end_clean();ob_start();的具体用法
    MySQL性能优化 (转载)
    遇到过的一些php笔试题
    php memcached 安装 install(转载)
    [转]mysql_fetch_row,mysql_fetch_array,mysql_fetch_object,mysql_fetch_assoc的区别
    深度探讨PHP之性能(看到的好文章,就转载啦)
  • 原文地址:https://www.cnblogs.com/-maybe/p/5496308.html
Copyright © 2011-2022 走看看