当前位置:网站首页>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);
}
}
边栏推荐
- 一键提取pdf中的表格
- When you open the browser, you will also open mango TV, Tiktok and other websites outside the home page
- Idea import / export settings file
- JDBC原理
- CSDN question and answer tag skill tree (I) -- Construction of basic framework
- Install mongdb tutorial and redis tutorial under Windows
- 导入 SQL 时出现 Invalid default value for ‘create_time‘ 报错解决方法
- The virtual machine Ping is connected to the host, and the host Ping is not connected to the virtual machine
- QT creator specify editor settings
- CSDN问答标签技能树(二) —— 效果优化
猜你喜欢

02-项目实战之后台员工信息管理

Installation and use of MySQL under MySQL 19 Linux

Data dictionary in C #
![[free setup] asp Net online course selection system design and Implementation (source code +lunwen)](/img/ac/b518796a92d00615cd374c0c835f38.jpg)
[free setup] asp Net online course selection system design and Implementation (source code +lunwen)

Mysql21 - gestion des utilisateurs et des droits

MySQL19-Linux下MySQL的安装与使用
![[recommended by bloggers] C # generate a good-looking QR code (with source code)](/img/5a/1dbafe5a28f016b815964b9b37c9f1.jpg)
[recommended by bloggers] C # generate a good-looking QR code (with source code)

Neo4j installation tutorial

QT creator specifies dependencies

Swagger、Yapi接口管理服务_SE
随机推荐
项目实战-后台员工信息管理(增删改查登录与退出)
QT creator specify editor settings
A brief introduction to the microservice technology stack, the introduction and use of Eureka and ribbon
Yum prompt another app is currently holding the yum lock; waiting for it to exit...
MySQL主從複制、讀寫分離
Postman uses scripts to modify the values of environment variables
QT creator specifies dependencies
Armv8-a programming guide MMU (2)
Asp access Shaoxing tourism graduation design website
安全测试涉及的测试对象
Ansible实战系列一 _ 入门
Copie maître - esclave MySQL, séparation lecture - écriture
数数字游戏
1. Mx6u learning notes (VII): bare metal development (4) -- master frequency and clock configuration
Software testing - interview question sharing
MySQL flush operation
LeetCode #461 汉明距离
NPM an error NPM err code enoent NPM err syscall open
csdn-Markdown编辑器
CSDN markdown editor