B2138 Maximum prime factor sequence problem solution

ideas

simulation.

  1. Fast reading. Needless to say about this.

  2. Determine prime numbers. This is used to help get the maximum prime factor later. The details are to avoid timeouts. The upper limit of the loop is sqrt (x)(remember to add the header file include<cmath>, unless you use the universal header ).

  3. Obtaining the largest prime factor. Enumerate from largest to smallest! ! Otherwise, an MLE warning will be issued.

  4. The main function has nothing to say, it is best to use printfit to avoid timeout.


Code

#include<bits/stdc++.h>
using namespace std;

int read (){
   int s = 0, x = 1;
   char ch = getchar ();
   while (ch < '0' or ch > '9'){if (ch == '-') x = -1; ch = getchar ();}
   while (ch >= '0' and ch <= '9'){s = s * 10 + ch - '0'; ch = getchar ();}
   return s * x;
}

bool pr (int x){
   for (int i = 2; i <= sqrt (x); i++) if (x % i == 0) return 0; 
   return 1;
}

int pd (int x){
   if (pr (x)) return x;
   for (int i = x - 1; i >= 2; i--){
   	if (!pr (i)) continue;
   	if (x % i == 0) return i;
   }
}
int n, m;
int main (){
   n = read (), m = read ();
   for (int i = n; i < m; i++)printf ("%d,",pd (i));
   printf ("%d\n", pd (m));
   return 0;
}

—— E n d End End——

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324052495&siteId=291194637