Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool isPrime(int n) {
- if (n < 2) return false;
- for (int i = 2; i * i <= n; i++) {
- if (n % i == 0) return false;
- }
- return true;
- }
- vector<int> findCycle(int p) {
- vector<int> cycle;
- map<int, bool> seen;
- int current = p % 10;
- while (!seen[current]) {
- cycle.push_back(current);
- seen[current] = true;
- current = (current * p) % 10;
- }
- return cycle;
- }
- int primeAssignment(int M, int N) {
- vector<int> primes;
- for (int i = M; i <= N; i++) {
- if (isPrime(i)) primes.push_back(i);
- }
- map<int, int> lastDigitCount;
- for (int prime : primes) {
- vector<int> cycle = findCycle(prime);
- for (int digit : cycle) {
- lastDigitCount[digit]++;
- }
- }
- int maxCount = 0;
- for (auto &entry : lastDigitCount) {
- maxCount = max(maxCount, entry.second);
- }
- return maxCount;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement