1. 程式人生 > >bzoj3643 Phi的反函數

bzoj3643 Phi的反函數

sin type color ++ sign void prime als 如果

傳送門:http://www.lydsy.com/JudgeOnline/problem.php?id=3643

【題解】

n = p1^a1*p2^a2*...*pm^am

phi(n) = p1(p1-1)^(a1-1)*p2(p2-1)^(a2-1)*...*pm^(am-1)

最多有10個不同的質因數就超過maxint了,這告訴我們可以搜索

我們假設p1<p2<p3<...<pm

那麽我們處理出<sqrt(n)的質數,因為我們只會用到這些質數來搜(因為其他平方完就爆炸了)

那麽我們就暴力嘗試是否被(pi-1)整除,是的話枚舉因子個數,dfs下去即可

註意特判如果我當前剩下了n,n+1是個質數,那麽也可以是直接當前答案*(n+1)

復雜度……我咋知道

O(能過)

技術分享
# include <math.h>
# include <stdio.h>
# include <string.h>
# include <iostream>
# include <algorithm>
// # include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int M = 5e4 + 10
; const int mod = 1e9+7; # define RG register # define ST static ll ans = 1e18; int n, F; bool isnp[M]; int p[M/3], pn=0; inline bool isprime(int n) { for (int i=1, to = sqrt(n); p[i] <= to; ++i) if(n % p[i] == 0) return false; return true; } inline void dfs(int n, int lst, ll sum) {
if(sum >= ans) return; if(n == 1) { ans = sum; return ; } if(n > F && isprime(n+1)) ans = min(ans, sum*(n+1)); for (int i=lst+1; p[i]-1 <= F && p[i]-1 <= n; ++i) { if(n % (p[i]-1) == 0) { int t = n/(p[i]-1); ll res = sum * p[i]; dfs(t, i, res); while(t % p[i] == 0) { t /= p[i]; res *= p[i]; dfs(t, i, res); } } } } int main() { cin >> n; F = sqrt(n); isnp[0] = isnp[1] = 1; for (int i=2; i<=50000; ++i) { if(!isnp[i]) p[++pn] = i; for (int j=1; j<=pn && i*p[j] <= 50000; ++j) { isnp[i*p[j]] = 1; if(i%p[j] == 0) break; } } dfs(n, 0, 1); if(ans <= 2147483647) cout << ans << endl; else cout << -1 << endl; return 0; }
View Code

bzoj3643 Phi的反函數