Description

我们知道,从区间[L,H](L和H为整数)中选取N个整数,总共有(H-L+1)^N种方案。小z很好奇这样选出的数的最大公约数的规律,他决定对每种方案选出的N个整数都求一次最大公约数,以便进一步研究。然而他很快发现工作量太大了,于是向你寻求帮助。你的任务很简单,小z会告诉你一个整数K,你需要回答他最大公约数刚好为K的选取方案有多少个。由于方案数较大,你只需要输出其除以1000000007的余数即可。

Input

输入一行,包含4个空格分开的正整数,依次为N,K,L和H。

Output

输出一个整数,为所求方案数。

Sample Input

2 2 2 4

Sample Output

3

HINT

样例解释

所有可能的选择方案:(2, 2), (2, 3), (2, 4), (3, 2), (3, 3), (3, 4), (4, 2), (4, 3), (4, 4)
其中最大公约数等于2的只有3组:(2, 2), (2, 4), (4, 2)
对于100%的数据,1≤N,K≤10^9,1≤L≤H≤10^9,H-L≤10^5

题解

设 $F(x)$ 为 $x\mid gcd$ 的个数, $f(x)$ 为 $gcd=x$ 的个数。

\begin{aligned}F(x)&=\sum_{x\mid d}f(d)\\\Rightarrow f(x)&=\sum_{x\mid d}\mu\left(\frac{d}{x}\right)F(d)\end{aligned}

对于输入 $(N,K,L,H)$ 我们记 $\left\lceil\frac{L}{K}\right\rceil$ 为 $l$ ,记 $\left\lfloor\frac{H}{K}\right\rfloor$ 为 $h$ 。

提出 $K$ ,答案就是 $$f(1)=\sum_{i=1}^{h}\mu(i)F(i)$$

显然 $F(i)=\left(\left\lfloor\frac{h}{i}\right\rfloor-\left\lfloor\frac{l-1}{i}\right\rfloor\right)^N$

由于 $h$ 很大,我们还是不能枚举这个 $i$ 。考虑到这样一个问题:当 $i$ 很大时 $\left\lfloor\frac{h}{i}\right\rfloor-\left\lfloor\frac{l-1}{i}\right\rfloor$ 会变成 $0$ 或 $1$ 。实际上只要 $i>h-l$ 数值就为 $0$ 或 $1$ 了。

那么现在答案就变成了 \begin{aligned}&\sum_{i=1}^{h-l}\mu(i)\left(\left\lfloor\frac{h}{i}\right\rfloor-\left\lfloor\frac{l-1}{i}\right\rfloor\right)^N+\sum_{i=h-l+1}^h\mu(i)\left\lfloor\frac{h}{i}\right\rfloor-\left\lfloor\frac{l-1}{i}\right\rfloor\\=&\sum_{i=1}^{h-l}\mu(i)\left(\left\lfloor\frac{h}{i}\right\rfloor-\left\lfloor\frac{l-1}{i}\right\rfloor\right)^N+\sum_{i=1}^h\mu(i)\left\lfloor\frac{h}{i}\right\rfloor-\left\lfloor\frac{l-1}{i}\right\rfloor-\sum_{i=1}^{h-l}\mu(i)\left\lfloor\frac{h}{i}\right\rfloor-\left\lfloor\frac{l-1}{i}\right\rfloor\end{aligned}

我们观察到式子 $\sum\limits_{i=1}^h\mu(i)\left\lfloor\frac{h}{i}\right\rfloor-\left\lfloor\frac{l-1}{i}\right\rfloor$ 的含义就是 $l\sim r$ 区间内有是否有值为 $1$ ,所以等价于 $[L\leq K\wedge K\leq H]$ 。

所以 $$ans=\sum_{i=1}^{h-l}\mu(i)\left(\left(\left\lfloor\frac{h}{i}\right\rfloor-\left\lfloor\frac{l-1}{i}\right\rfloor\right)^N-\left(\left\lfloor\frac{h}{i}\right\rfloor-\left\lfloor\frac{l-1}{i}\right\rfloor\right)\right)+[L\leq K\wedge K\leq H]$$

 //It is made by Awson on 2018.1.24
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define Abs(a) ((a) < 0 ? (-(a)) : (a))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
#define writeln(x) (write(x), putchar('\n'))
#define lowbit(x) ((x)&(-(x)))
using namespace std;
const int N = 1e5;
const int MOD = ;
void read(int &x) {
char ch; bool flag = ;
for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || ); ch = getchar());
for (x = ; isdigit(ch); x = (x<<)+(x<<)+ch-, ch = getchar());
x *= -*flag;
}
void write(LL x) {
if (x > ) write(x/);
putchar(x%+);
} int n, k, l, h, mu[N+];
int isprime[N+], prime[N+], tot; void get_mu() {
memset(isprime, , sizeof(isprime)); isprime[] = , mu[] = ;
for (int i = ; i <= N; i++) {
if (isprime[i]) prime[++tot] = i, mu[i] = -;
for (int j = ; j <= tot && i*prime[j] <= N; j++) {
isprime[i*prime[j]] = ;
if (i%prime[j]) mu[i*prime[j]] = -mu[i];
else {mu[i*prime[j]] = ; break; }
}
}
}
int quick_pow(int a, int b) {
int ans = ;
while (b) {
if (b&) ans = (LL)ans*a%MOD;
a = (LL)a*a%MOD, b >>= ;
}
return ans;
}
void work() {
get_mu(); read(n), read(k), read(l), read(h);
int flag = (l <= k && k <= h), ans = ;
l = ceil(.*l/k), h = (h/k);
for (int i = ; i <= h-l; i++) ans = (ans+(LL)mu[i]*quick_pow(h/i-(l-)/i, n)%MOD)%MOD;
for (int i = ; i <= h-l; i++) ans = (ans-(LL)mu[i]*(h/i-(l-)/i)%MOD)%MOD;
writeln((ans+flag+MOD)%MOD);
}
int main() {
work();
return ;
}

[CQOI 2015]选数的更多相关文章

  1. [BZOJ 3930] [CQOI 2015]选数(莫比乌斯反演+杜教筛)

    [BZOJ 3930] [CQOI 2015]选数(莫比乌斯反演+杜教筛) 题面 我们知道,从区间\([L,R]\)(L和R为整数)中选取N个整数,总共有\((R-L+1)^N\)种方案.求最大公约数 ...

  2. 解题:CQOI 2015 选数

    题面 神仙题,不需要反演 首先上下界同时除以$k$,转换成取$n$个$gcd$为$1$的数的方案数,其中上界向下取整,下界向上取整 然后设$f[i]$表示选$n$个互不相同的数$gcd$为$i$的方案 ...

  3. Bzoj3930: [CQOI 2015] 选数 & COGS2699: [CQOI 2015] 选数加强版

    题面 Bzoj COGS加强版 Sol 非加强版可以枚举AC这里不再讲述 设\(f(i)\)表示在\([L, H]\)取\(N\)个,\(gcd为i\)的方案数 \(F(i)=\sum_{i|d}f( ...

  4. 【BZOJ-2732】集合选数 状压DP (思路题)

    2734: [HNOI2012]集合选数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1070  Solved: 623[Submit][Statu ...

  5. CODE VS1008选数

    #include<cstdlib> #include<cstdio> #include<iostream> #include<cmath> #inclu ...

  6. BZOJ 3930: [CQOI2015]选数 递推

    3930: [CQOI2015]选数 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pro ...

  7. bzoj 2734: [HNOI2012]集合选数 状压DP

    2734: [HNOI2012]集合选数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 560  Solved: 321[Submit][Status ...

  8. BZOJ3930: [CQOI2015]选数

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3930 容斥原理. 令l=(L-1)/k,r=R/k,这样找k的倍数就相当于找1的倍数. 设F[ ...

  9. 【BZOJ3930】选数(莫比乌斯反演,杜教筛)

    [BZOJ3930]选数(莫比乌斯反演,杜教筛) 题面 给定\(n,K,L,R\) 问从\(L-R\)中选出\(n\)个数,使得他们\(gcd=K\)的方案数 题解 这样想,既然\(gcd=K\),首 ...

随机推荐

  1. java 5线程中 Semaphore信号灯,CyclicBarrier类,CountDownLatch计数器以及Exchanger类使用

    先来讲解一下Semaphore信号灯的作用:  可以维护当前访问自身的线程个数,并提供了同步机制, 使用semaphore可以控制同时访问资源的线程个数 例如,实现一个文件允许的并发访问数. 请看下面 ...

  2. C语言——总结回顾

    1.当初你是如何做出选择计算机专业的决定的?经过一个学期,你的看法改变了么,为什么? 你觉得计算机是你喜欢的领域吗,它是你擅长的领域吗? 为什么? 答:①当初选择计算机专业是出于一种选择,是一种带着冲 ...

  3. 第二次作业:APP案例分析

    App案例分析 产品:三国杀-页游手游双通 选择理由 当今社会手机已经渐渐取代了电脑在人们日常生活的需求,既然要选择APP进行案例分析,首推的估计就是手机APP了.三国杀是陪伴我高中时代的主要娱乐方式 ...

  4. 【Swift】 iOS开发容易产生Bug的地方

    1.隐藏navigationBar(尤其是多级隐藏) 2.共用collectionView或tableView 3.继承关系下,注意覆写父类时的super方法的调用 4.关于权限的问题(相机权限,相册 ...

  5. ASP.NET MVC编程——单元测试

    1自动化测试基本概念 自动化测试分为:单元测试,集成测试,验收测试. 单元测试 检验被测单元的功能,被测单元一般为低级别的组件,如一个类或类方法. 单元测试要满足四个条件:自治的,可重复的,独立的,快 ...

  6. CSS <input type="file">样式设置

    这是最终想要的效果~~~ 实现很简单,div设置背景图片,<input type="file"/>绝对定位上去再设置opacity:0(透明度为0 ) 直接上代码,希望 ...

  7. 【转】Python处理wave文件

    #本文PDF版下载 Python解析Wav文件并绘制波形的方法 #本文代码下载 Wav波形绘图代码 #本文实例音频文件night.wav下载 音频文件下载 (石进-夜的钢琴曲) 前言 在现在繁忙的生活 ...

  8. Python struct模块

    有的时候需要用python处理二进制数据,比如,存取文件,socket操作时.这时候,可以使用python的struct模块来完成.可以用 struct来处理c语言中的结构体. struct模块中最重 ...

  9. 第五章 JavaScript对象及初识面向对象

    第五章   JavaScript对象及初识面向对象 一.对象 在JavaScript中,所有事物都是对象,如字符串.数值.数组.函数等. 在JavaScript对象分为内置对象和自定义对象,要处理一些 ...

  10. hadoop大数据技术架构详解

    大数据的时代已经来了,信息的爆炸式增长使得越来越多的行业面临这大量数据需要存储和分析的挑战.Hadoop作为一个开源的分布式并行处理平台,以其高拓展.高效率.高可靠等优点越来越受到欢迎.这同时也带动了 ...