Advertisement
asdfg0998

Untitled

Nov 20th, 2024
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. bool isPrime(int n) {
  2.     if (n < 2) return false;
  3.     for (int i = 2; i * i <= n; i++) {
  4.         if (n % i == 0) return false;
  5.     }
  6.     return true;
  7. }
  8.  
  9. vector<int> findCycle(int p) {
  10.     vector<int> cycle;
  11.     map<int, bool> seen;
  12.     int current = p % 10;
  13.    
  14.     while (!seen[current]) {
  15.         cycle.push_back(current);
  16.         seen[current] = true;
  17.         current = (current * p) % 10;
  18.     }
  19.     return cycle;
  20. }
  21.  
  22. int primeAssignment(int M, int N) {
  23.     vector<int> primes;
  24.     for (int i = M; i <= N; i++) {
  25.         if (isPrime(i)) primes.push_back(i);
  26.     }
  27.  
  28.     map<int, int> lastDigitCount;
  29.     for (int prime : primes) {
  30.         vector<int> cycle = findCycle(prime);
  31.         for (int digit : cycle) {
  32.             lastDigitCount[digit]++;
  33.         }
  34.     }
  35.  
  36.     int maxCount = 0;
  37.     for (auto &entry : lastDigitCount) {
  38.         maxCount = max(maxCount, entry.second);
  39.     }
  40.  
  41.     return maxCount;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement