URAL 1748 The Most Complex Number

题目链接:https://vjudge.net/problem/11177

题目大意:

  求小于等于 n 的最大反素数。

分析:

  n <= 10^8,而100以内素数的乘积超过10^36,因此先求出100以内的素数,再dfs寻找最大反素数。

代码如下:

  1 #pragma GCC optimize("Ofast")
  2 #include <bits/stdc++.h>
  3 using namespace std;
  4  
  5 #define INIT() std::ios::sync_with_stdio(false);std::cin.tie(0);
  6 #define Rep(i,n) for (int i = 0; i < (n); ++i)
  7 #define For(i,s,t) for (int i = (s); i <= (t); ++i)
  8 #define rFor(i,t,s) for (int i = (t); i >= (s); --i)
  9 #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
 10 #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
 11 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
 12 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
 13  
 14 #define pr(x) cout << #x << " = " << x << "  "
 15 #define prln(x) cout << #x << " = " << x << endl
 16  
 17 #define LOWBIT(x) ((x)&(-x))
 18  
 19 #define ALL(x) x.begin(),x.end()
 20 #define INS(x) inserter(x,x.begin())
 21  
 22 #define ms0(a) memset(a,0,sizeof(a))
 23 #define msI(a) memset(a,inf,sizeof(a))
 24 #define msM(a) memset(a,-1,sizeof(a))
 25 
 26 #define MP make_pair
 27 #define PB push_back
 28 #define ft first
 29 #define sd second
 30  
 31 template<typename T1, typename T2>
 32 istream &operator>>(istream &in, pair<T1, T2> &p) {
 33     in >> p.first >> p.second;
 34     return in;
 35 }
 36  
 37 template<typename T>
 38 istream &operator>>(istream &in, vector<T> &v) {
 39     for (auto &x: v)
 40         in >> x;
 41     return in;
 42 }
 43  
 44 template<typename T1, typename T2>
 45 ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
 46     out << "[" << p.first << ", " << p.second << "]" << "\n";
 47     return out;
 48 }
 49  
 50 typedef long long LL;
 51 typedef unsigned long long uLL;
 52 typedef pair< double, double > PDD;
 53 typedef pair< int, int > PII;
 54 typedef pair< LL, LL > PLL;
 55 typedef set< int > SI;
 56 typedef vector< int > VI;
 57 typedef map< int, int > MII;
 58 typedef vector< LL > VL;
 59 typedef vector< VL > VVL;
 60 const double EPS = 1e-10;
 61 const int inf = 1e9 + 9;
 62 const LL mod = 1e9 + 7;
 63 const int maxN = 5e5 + 7;
 64 const LL ONE = 1;
 65 const LL evenBits = 0xaaaaaaaaaaaaaaaa;
 66 const LL oddBits = 0x5555555555555555;
 67 
 68 int T;
 69 LL n;
 70 PLL ans;
 71 
 72 // 欧拉筛法 
 73 const int N = 100;
 74 bool isPrime[N + 7];//isPrime[i]表示i是不是质数 
 75 VI primes;//primes用来存质数 
 76 inline void Euler(){
 77     For(i, 2, N) isPrime[i] = true;//初始化为质数 
 78     For(i, 2, N) {
 79         if(isPrime[i]) primes.PB(i);//把质数存起来 
 80         for(int j = 0; j < primes.size() && i * primes[j] <= N; ++j) {
 81             isPrime[i * primes[j]] = false;
 82             if(i % primes[j] == 0) break;//保证每个合数被它最小的质因数筛去 
 83         }
 84     }    
 85 }
 86 
 87 // x 表示当前处理到第 x 个质数
 88 // ret为当前选择下的质数乘积 
 89 // fcnt 为 1~x-1 个质数中,每个质数选择数量+1的乘积 
 90 // prev_cnt表示第 x-1 个质数已经选了多少个 
 91 // cnt表示第 x 个质数已经选了多少个 
 92 inline void dfs(int x, LL ret, LL fcnt, int prev_cnt, int cnt) {
 93     if(ret > n || prev_cnt < cnt) return;
 94     
 95     if(ans.sd < fcnt * (cnt + 1)) ans = MP(ret, fcnt * (cnt + 1));
 96     else if(ans.sd == fcnt * (cnt + 1) && ans.ft > ret) ans = MP(ret, fcnt * (cnt + 1));
 97     
 98     if(n / ret >= primes[x])dfs(x, ret * primes[x], fcnt, prev_cnt, cnt + 1); // 选 primes[x]
 99     if(cnt) dfs(x + 1, ret, fcnt * (cnt + 1), cnt, 0); // 不选 primes[x]
100 }
101 
102 int main(){
103     INIT();
104     cin >> T;
105     Euler(); // 预先求出100以内的素数 
106     while(T--) {
107         cin >> n;
108         ans = MP(inf, -1);
109         dfs(0, 1, 1, inf, 0);
110         cout << ans.ft << " " << ans.sd << endl;
111     }
112     return 0;
113 }
View Code

猜你喜欢

转载自www.cnblogs.com/zaq19970105/p/10800152.html