对于d, 记{ai}中是d的倍数的数的个数为c, 那么有:

直接计算即可,复杂度O(NlogN+MlogM)

---------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<algorithm>
 
using namespace std;
 
typedef long long ll;
 
const int MOD = 1000000007;
const int maxn = 300009;
 
int ans[maxn];
int N, M, K, seq[maxn], cnt[maxn];
int Inv[maxn], Fac[maxn];
 
void gcd(int a, int b, int &d, int &x, int &y) {
if(!b) {
d = a;
x = 1;
y = 0;
} else {
gcd(b, a % b, d, y, x);
y -= x * (a / b);
}
}
 
int INV(int v) {
int d, x, y;
gcd(v, MOD, d, x, y);
return (x + MOD) % MOD;
}
 

void Init() {

Inv[0] = INV(Fac[0] = 1);
for(int i = 1; i <= N; i++) {
Fac[i] = ll(i) * Fac[i - 1] % MOD;
Inv[i] = INV(Fac[i]);
}
}
 
int C(int m, int n) {
return ll(Fac[n]) * Inv[n - m] % MOD * Inv[m] % MOD;
}
 

int Power(int x, int t) {

int ret = 1;
for(; t; t >>= 1, x = ll(x) * x % MOD)
if(t & 1) ret = ll(x) * ret % MOD;
return ret;
}
 

int main() {

memset(cnt, 0, sizeof cnt);
scanf("%d%d%d", &N, &M, &K);
for(int i = 0; i < N; i++) {
scanf("%d", seq + i);
cnt[seq[i]]++;
}
Init();
for(int i = M; i; i--) {
int c = 0;
for(int j = i; j <= M; j += i)
if(cnt[j]) c += cnt[j];
if(c + K < N) {
ans[i] = 0; continue;
}
ans[i] = (ll) Power(M / i, N - c) * C(N - K, c) % MOD * Power(M / i - 1, c - N + K) % MOD;
for(int j = i << 1; j <= M; j += i)
if((ans[i] -= ans[j]) < 0) ans[i] += MOD;
}
printf("%d", ans[1]);
for(int i = 2; i <= M; i++)
printf(" %d", ans[i]);
return 0;
}

---------------------------------------------------------------------------

4305: 数列的GCD

Time Limit: 10 Sec  Memory Limit: 256 MB
Submit: 149  Solved: 68
[Submit][Status][Discuss]

Description

给出一个长度为N的数列{a[n]},1<=a[i]<=M(1<=i<=N)。 
现在问题是,对于1到M的每个整数d,有多少个不同的数列b[1], b[2], ..., b[N],满足: 
(1)1<=b[i]<=M(1<=i<=N); 
(2)gcd(b[1], b[2], ..., b[N])=d; 
(3)恰好有K个位置i使得a[i]<>b[i](1<=i<=N) 
注:gcd(x1,x2,...,xn)为x1, x2, ..., xn的最大公约数。 
输出答案对1,000,000,007取模的值。 

Input

第一行包含3个整数,N,M,K。 
第二行包含N个整数:a[1], a[2], ..., a[N]。 

Output

输出M个整数到一行,第i个整数为当d=i时满足条件的不同数列{b[n]}的数目mod 1,000,000,007的值。 

Sample Input

3 3 3
3 3 3

Sample Output

7 1 0

HINT

当d=1,{b[n]}可以为:(1, 1, 1), (1, 1, 2), (1, 2, 1), (1, 2, 2), (2, 1, 1), (2, 1, 2), (2, 2, 1)。 

当d=2,{b[n]}可以为:(2, 2, 2)。 

当d=3,因为{b[n]}必须要有k个数与{a[n]}不同,所以{b[n]}不能为(3, 3, 3),满足条件的一个都没有。 

对于100%的数据,1<=N,M<=300000, 1<=K<=N, 1<=a[i]<=M。 

Source

BZOJ 4305: 数列的GCD( 数论 )的更多相关文章

  1. bzoj 4305 数列的GCD

    LINK:数列的GCD 题意: 给出一个长度为N的数列{a[n]},1<=a[i]<=M(1<=i<=N). 现在问题是,对于1到M的每个整数d,有多少个不同的数列b[1], ...

  2. 【BZOJ 4305】 4305: 数列的GCD (数论)

    4305: 数列的GCD Description 给出一个长度为N的数列{a[n]},1<=a[i]<=M(1<=i<=N).  现在问题是,对于1到M的每个整数d,有多少个不 ...

  3. BZOJ 2820: YY的GCD | 数论

    题目: 题解: http://hzwer.com/6142.html #include<cstdio> #include<algorithm> #define N 100000 ...

  4. [BZOJ 2820] YY的gcd(莫比乌斯反演+数论分块)

    [BZOJ 2820] YY的gcd(莫比乌斯反演+数论分块) 题面 给定N, M,求\(1\leq x\leq N, 1\leq y\leq M\)且gcd(x, y)为质数的(x, y)有多少对. ...

  5. bzoj 2818 GCD 数论 欧拉函数

    bzoj[2818]Gcd Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. Input 一个整数N Output 如题 Samp ...

  6. 【莫比乌斯反演】关于Mobius反演与gcd的一些关系与问题简化(bzoj 2301 Problem b&&bzoj 2820 YY的GCD&&BZOJ 3529 数表)

    首先我们来看一道题  BZOJ 2301 Problem b Description 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd( ...

  7. bzoj 4303 数列

    bzoj 4303 数列 二维 \(KD-Tree\) 模板题. \(KD-Tree\) 虽然在更新和查询的方式上类似于线段树,但其本身定义是类似于用 \(splay/fhq\ treap\) 维护区 ...

  8. Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论

    Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变 ...

  9. [BZOJ 2989]数列(二进制分组+主席树)

    [BZOJ 2989]数列(二进制分组+主席树) 题面 给定一个长度为n的正整数数列a[i]. 定义2个位置的graze值为两者位置差与数值差的和,即graze(x,y)=|x-y|+|a[x]-a[ ...

随机推荐

  1. c语言libcurl 使用实例get/post方法+c语言字符串处理

    #include <stdio.h> #include <curl/curl.h> #include <string.h> #include <ctype.h ...

  2. 字符串比较必须使用strcmp

    char s1[]="this" char *s2 = "this" if(s1=="this"){ printf("s1 is ...

  3. Pascal 线段树 lazy-tag 模板

    先说下我的代码风格(很丑,勿喷) maxn表示最大空间的四倍 tree数组表示求和的线段树 delta表示增减的增量标记 sign表示覆盖的标记 delta,sign实际上都是lazy标志 pushd ...

  4. javaio学习笔记-字符流类(2)

    1.java.io包中的字符流类-FileReader和FileWriter: BufferedReader:缓存的输入字符流; BufferedWriter:缓存的输出字符流; FileReader ...

  5. Python之美[从菜鸟到高手]--生成器之全景分析

    yield指令,可以暂停一个函数并返回中间结果.使用该指令的函数将保存执行环境,并且在必要时恢复. 生成器比迭代器更加强大也更加复杂,需要花点功夫好好理解贯通. 看下面一段代码: def gen(): ...

  6. Android TXT文件读写

    package com.wirelessqa.helper; import java.io.FileInputStream; import java.io.FileOutputStream; impo ...

  7. linux杂记(七)linux档案与目录管理指令

    1.目录的相关操作:cd,pwd,mkdir,rmdir 路径(PATH): 绝对路径:路径的写法[一定由根目录/写起],例如/usr/share/doc这个目录 相对路径:路径的写法[不是由/写起] ...

  8. php功能---删除空目录

    header('content-type:text/html;charset:utf-8'); function display($dir){ //判断是否是一个目录 if(!is_dir($dir) ...

  9. codevs 1183 泥泞的道路 01分数规划

    题目链接 题目描述 Description CS有n个小区,并且任意小区之间都有两条单向道路(a到b,b到a)相连.因为最近下了很多暴雨,很多道路都被淹了,不同的道路泥泞程度不同.小A经过对近期天气和 ...

  10. poj 2155 matrix 二维线段树

    题目链接 区间翻转, 单点查询, 查询操作我真是不太明白...... #include <iostream> #include <vector> #include <cs ...