sudama_bera

Untitled

Jun 8th, 2026
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.99 KB | Source Code | 0 0
  1. int maxScore(vector<int>& nums, int maxVal) {
  2.         int n=nums.size();
  3.         int max_elem=maxVal;
  4.         for (int num:nums) {
  5.             max_elem=max(max_elem,num);
  6.         }
  7.         vector<int>spf(max_elem+1);
  8.         for (int i=2;i<=max_elem;i++)spf[i]=i;
  9.         for (int i=2;i*i<=max_elem;i++) {
  10.             if (spf[i]==i){
  11.                 for (int j=i*i;j<=max_elem; j += i) {
  12.                     if (spf[j]==j)spf[j]=i;
  13.                 }
  14.             }
  15.         }
  16.         vector<int>count_multiples(max_elem+1,0);
  17.         vector<bool>in_nums(max_elem+1,false);
  18.         for (int num:nums) {
  19.             in_nums[num]=true;
  20.             if (num==1)continue;
  21.             vector<int> factors;
  22.             int temp=num;
  23.             while (temp>1) {
  24.                 int p=spf[temp];
  25.                 factors.push_back(p);
  26.                 while(temp%p==0)temp/= p;
  27.             }
  28.             int sz=factors.size();
  29.             for (int mask=1;mask<(1<<sz);mask++) {
  30.                 int prod=1;
  31.                 for (int i=0;i<sz;i++) {
  32.                     if ((mask>>i)&1) {
  33.                         prod*=factors[i];
  34.                     }
  35.                 }
  36.                 count_multiples[prod]++;
  37.             }
  38.         }
  39.        
  40.      
  41.         auto get_not_coprime_count=[&](int x) {
  42.             if (x==1)return 0;
  43.             vector<int>factors;
  44.             int temp=x;
  45.             while (temp>1) {
  46.                 int p = spf[temp];
  47.                 factors.push_back(p);
  48.                 while (temp % p == 0) temp /= p;
  49.             }
  50.            
  51.             int sz = factors.size();
  52.             int res = 0;
  53.             for (int mask = 1; mask < (1 << sz); mask++) {
  54.                 int prod = 1;
  55.                 int bits = 0;
  56.                 for (int i = 0; i < sz; i++) {
  57.                     if ((mask >> i) & 1) {
  58.                         prod *= factors[i];
  59.                         bits++;
  60.                     }
  61.                 }
  62.                 if (bits%2==1) res+=count_multiples[prod];
  63.                 else res-=count_multiples[prod];
  64.             }
  65.             return res;
  66.         };
  67.         vector<int> pos_x;
  68.         for (int i = 1;i<=maxVal;i++) {
  69.             pos_x.push_back(i);
  70.         }
  71.         for (int num:nums) {
  72.             if (num>maxVal) {
  73.                 pos_x.push_back(num);
  74.             }
  75.         }
  76.         sort(pos_x.begin(), pos_x.end());
  77.         pos_x.erase(unique(pos_x.begin(), pos_x.end()),pos_x.end());
  78.         int max_score = INT_MIN;
  79.         for (int x : pos_x) {
  80.             if (x == 1) {
  81.                 int cost=in_nums[1]?0:1;
  82.                 max_score=max(max_score,x-cost);
  83.                 continue;
  84.             }
  85.            
  86.             int C = get_not_coprime_count(x);
  87.             int cost = 0;
  88.             if (in_nums[x]) {
  89.                 cost = C - 1;
  90.             } else {
  91.                 cost=(C==0)?1:C;
  92.             }
  93.             max_score = max(max_score, x - cost);
  94.         }
  95.        
  96.         return max_score;
  97.     }
Advertisement
Add Comment
Please, Sign In to add comment