1. 程式人生 > 實用技巧 >Acwing 1294 櫻花

Acwing 1294 櫻花

題意:

給出一個整數n,求有多少個正整數對(x, y)滿足$\frac{1}{x}$ + $\frac{1} {y}$ = $\frac{1} {n!}$.答案對$10^9+7$取模

思路:

公式推導:

原式 =$\frac{x + y}{xy}$ =$\frac{1}{n!}$

$xn! + yn! = xy$

$xn! = xy - yn!$

$y = \frac{xn!}{x - n!}$

則,$y = \frac{xn!}{x - n!} = \frac{(x - n! + n!)n!}{x - n!} = n! + \frac{n! ^ 2}{x - n!}$

問題轉變為$n!^2$的約數個數

Code:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long ll;

const int N = 1e6 + 10;
const int mod = 1e9 + 7;

int n;
int primes[N], cnt;
bool st[N];

void get(int n){
    for(int i = 2; i <= n; i ++){
        
if(!st[i]){ primes[cnt ++] = i; } for(int j = 0; primes[j] <= n / i; j ++){ st[i * primes[j]] = true; if(i % primes[j] == 0){ break; } } } } int main(){ cin.tie(0); cout.tie(0); get(N); cin >> n;
int ans = 1; for(int i = 0; i < cnt; i ++){ int s = 0, p = primes[i], num = 1; while(n >= (ll)num * p){ num *= p; s += n / num; } ans = (ll)ans * (2 * s + 1) % mod; } cout << ans << endl; return 0; }