Ankit_132

B

Jun 20th, 2024
802
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long
  4.  
  5. bool isprime(int n) {
  6.     if (n <= 1) return false;
  7.     if (n == 2) return true;
  8.     if (n % 2 == 0) return false;
  9.     for (int i = 3; i <= sqrt(n); i += 2) {
  10.         if (n % i == 0) {
  11.             return false;
  12.         }
  13.     }
  14.     return true;
  15. }
  16.  
  17. vector<int> precomputeSumOfPrimes(int max_n) {
  18.     vector<int> v(max_n + 1, 0);
  19.     int sum = 0;
  20.     for (int i = 2; i <= max_n; ++i) {
  21.         if (isprime(i)) {
  22.             sum += i;
  23.         }
  24.         v[i] = sum;
  25.     }
  26.     return v;
  27. }
  28.  
  29. int32_t main() {
  30.     ios_base::sync_with_stdio(false);
  31.     cin.tie(NULL);
  32.  
  33.     int t;
  34.     cin >> t;
  35.  
  36.     vector<int> v = precomputeSumOfPrimes(1e6);
  37.     while(t--){
  38.        
  39.     int n;
  40.     cin>>n;
  41.         if (isprime(n)) {
  42.             cout << v[n ] * n << endl;
  43.         } else {
  44.             if(n%2==0){
  45.                 cout << 2 * n << endl;
  46.             }
  47.             else{
  48.                 int p=0;
  49.                for( p=3;p*p<=n;p++){
  50.                    if(n%p==0)break;
  51.                }
  52.                cout<<v[p]*n<<endl;
  53.             }
  54.         }
  55.     }
  56.  
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment