当前位置:网站首页>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);
}
}
边栏推荐
- 记某公司面试算法题:查找一个有序数组某个数字出现的次数
- Deoldify项目问题——OMP:Error#15:Initializing libiomp5md.dll,but found libiomp5md.dll already initialized.
- [Li Kou 387] the first unique character in the string
- QT creator create button
- CSDN question and answer tag skill tree (I) -- Construction of basic framework
- MySQL21-用戶與權限管理
- 自动机器学习框架介绍与使用(flaml、h2o)
- Mysql22 logical architecture
- [recommended by bloggers] C WinForm regularly sends email (with source code)
- Postman environment variable settings
猜你喜欢
Win10: how to modify the priority of dual network cards?
A brief introduction to the microservice technology stack, the introduction and use of Eureka and ribbon
Use dapr to shorten software development cycle and improve production efficiency
API learning of OpenGL (2002) smooth flat of glsl
Some problems in the development of unity3d upgraded 2020 VR
【博主推荐】SSM框架的后台管理系统(附源码)
软件测试与质量学习笔记3--白盒测试
CSDN question and answer module Title Recommendation task (I) -- Construction of basic framework
QT creator specify editor settings
Did you forget to register or load this tag 报错解决方法
随机推荐
解决:log4j:WARN Please initialize the log4j system properly.
[free setup] asp Net online course selection system design and Implementation (source code +lunwen)
Error reporting solution - io UnsupportedOperation: can‘t do nonzero end-relative seeks
csdn-Markdown编辑器
[recommended by bloggers] C WinForm regularly sends email (with source code)
Csdn-nlp: difficulty level classification of blog posts based on skill tree and weak supervised learning (I)
【博主推荐】SSM框架的后台管理系统(附源码)
QT creator shape
JDBC原理
01项目需求分析 (点餐系统)
Some problems in the development of unity3d upgraded 2020 VR
【博主推荐】C# Winform定时发送邮箱(附源码)
Mysql21 user and permission management
Introduction to the easy copy module
报错解决 —— io.UnsupportedOperation: can‘t do nonzero end-relative seeks
CSDN问答模块标题推荐任务(一) —— 基本框架的搭建
La table d'exportation Navicat génère un fichier PDM
Unable to call numpy in pycharm, with an error modulenotfounderror: no module named 'numpy‘
++Implementation of I and i++
【博主推荐】asp.net WebService 后台数据API JSON(附源码)