当前位置:网站首页>AcWing 1294.樱花 题解
AcWing 1294.樱花 题解
2022-07-06 09:14:00 【爱吃章鱼的怪兽】
AcWing 1294.樱花 题解
题目描述
给定一个整数 n n n ,求有多少正整数对 ( x , y ) (x,y) (x,y) 满足 1 x + 1 y = 1 n ! \dfrac{1}{x}+\dfrac{1}{y}=\dfrac{1}{n!} x1+y1=n!1
输入格式:
一个整数 n n n
输出格式:
一个整数,表示满足条件的数对数量
答案对 1 0 9 + 7 10^9+7 109+7 取模
数据范围
1 ≤ n ≤ 1 0 6 1\le n \le 10^6 1≤n≤106
题解
观察式子,容易推出 x > n ! x > n! x>n! y > n ! y >n! y>n!
两个未知量 x , y x,y x,y, 知道其中一个后就能够推出另外一个
因此我们不妨设 y = n ! + k y=n!+k y=n!+k
变换后的式子为
1 x + 1 n ! + k = 1 n ! \dfrac{1}{x}+\dfrac{1}{n!+k}=\dfrac{1}{n!} x1+n!+k1=n!1
将式子两边通分可得 n ! ( n ! + k ) + ( n ! ) x = x ( n ! + k ) n!(n!+k)+(n!)x=x(n!+k) n!(n!+k)+(n!)x=x(n!+k)
变换可得
x = n ! ( n ! + k ) k = ( n ! ) 2 k + n ! x=\dfrac{n!(n!+k)}{k}=\dfrac{(n!)^2}{k}+n! x=kn!(n!+k)=k(n!)2+n!
由于x要满足正整数这一特性,所以问题转换成了k的值需要变成 ( n ! ) 2 (n!)^2 (n!)2的约数
问题变成了求一个数的约数
求约数可以用 算数基本定理
算术基本定理如下
任何一个正整数都可以用它的质因子唯一确定(其中 p i p_i pi为其质因子)
x = p 1 α 1 ⋅ p 2 α 2 ⋅ p 3 α 3 … ⋅ p n α n x=p_1^{\alpha_1} · p_2^{\alpha_2}·p_3^{\alpha_3}\dots·p_n^{\alpha_n} x=p1α1⋅p2α2⋅p3α3…⋅pnαn
根据全排列公式则其因子个数为
( α 1 + 1 ) ⋅ ( α 2 + 1 ) ⋅ ( α 2 + 1 ) ⋅ ( α 2 + 1 ) (\alpha_1+1)·(\alpha_2+1)·(\alpha_2+1)·(\alpha_2+1) (α1+1)⋅(α2+1)⋅(α2+1)⋅(α2+1)
问题变成了求解 ( n ! ) 2 (n!)^2 (n!)2的质因子和每个质因子的阶数
我们可以先求 ( n ! ) (n!) (n!)的质因子和每个质因子的阶数后将阶数乘2就可以的到 ( n ! ) 2 (n!)^2 (n!)2的解
如何求 ( n ! ) (n!) (n!)的质因子和质因子的阶数可以参考我的另一篇博客
求得 ( n ! ) (n!) (n!)的所有质因子后将质因子的阶数乘2,最后利用因子个数的计算公式就可以得到k的所有取值
对于每一个k值都有一个x值与其对应,则最后的所有取值即为正整数数对的个数
完整代码
import java.io.*;
import java.util.*;
public class Main {
static int n;
static final int N=1000010;
static final long MOD=1000000007;
static int[] Primes =new int[N];
static boolean[] isPrime =new boolean[N];
static int cnt=0;
static long ans=1;
public static void init(int n)//线性筛
{
Arrays.fill(isPrime,true);
for(int i=2;i<=n;i++) {
if (isPrime[i])
Primes[cnt++] = i;
for(int j=0;j<cnt;j++)
{
int p=Primes[j];
if(i*p>n)
break;
isPrime[i*p]=false;
if(i%p==0)
{
break;
}
}
}
}
public static void main(String[] agrs) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
n=Integer.parseInt(reader.readLine());
init(n);
for(int i=0;i<cnt;i++)//分解质因子
{
int tmp=n;
int p=Primes[i];
long res=0;
while(tmp>0)
{
res+=tmp/p;
tmp/=p;
}
res*=2;//转换成平方的质因子阶数
ans=(ans%MOD)*((res+1)%MOD);//因子计算公式
ans%=MOD;
}
System.out.println(ans);
}
}
边栏推荐
- [BMZCTF-pwn] 12-csaw-ctf-2016-quals hungman
- neo4j安装教程
- Windows cannot start the MySQL service (located on the local computer) error 1067 the process terminated unexpectedly
- NPM an error NPM err code enoent NPM err syscall open
- windows无法启动MYSQL服务(位于本地计算机)错误1067进程意外终止
- Development of C language standard
- [recommended by bloggers] background management system of SSM framework (with source code)
- frp内网穿透那些事
- QT creator specifies dependencies
- 虚拟机Ping通主机,主机Ping不通虚拟机
猜你喜欢
Django运行报错:Error loading MySQLdb module解决方法
Unable to call numpy in pycharm, with an error modulenotfounderror: no module named 'numpy‘
基于apache-jena的知识问答
CSDN问答标签技能树(五) —— 云原生技能树
Other new features of mysql18-mysql8
[recommended by bloggers] C # generate a good-looking QR code (with source code)
API learning of OpenGL (2002) smooth flat of glsl
Postman Interface Association
IDEA 导入导出 settings 设置文件
MySQL19-Linux下MySQL的安装与使用
随机推荐
CSDN question and answer tag skill tree (II) -- effect optimization
[recommended by bloggers] background management system of SSM framework (with source code)
JDBC principle
++Implementation of I and i++
MySQL flush operation
数数字游戏
02-项目实战之后台员工信息管理
Image recognition - pyteseract TesseractNotFoundError: tesseract is not installed or it‘s not in your path
Ansible practical Series II_ Getting started with Playbook
Error reporting solution - io UnsupportedOperation: can‘t do nonzero end-relative seeks
Summary of numpy installation problems
Other new features of mysql18-mysql8
CSDN blog summary (I) -- a simple first edition implementation
Swagger、Yapi接口管理服务_SE
Ansible practical Series III_ Task common commands
Installation and use of MySQL under MySQL 19 Linux
Swagger, Yapi interface management service_ SE
Unable to call numpy in pycharm, with an error modulenotfounderror: no module named 'numpy‘
A brief introduction to the microservice technology stack, the introduction and use of Eureka and ribbon
虚拟机Ping通主机,主机Ping不通虚拟机