Help me solve "SUM of N" or "COUNTN"

Question link :

Please tell me what have I done wrong?

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

bool isPrime(long long x){
    for(long long i=2; i<=sqrt(x); i++){
        if(x%i==0){
            return false;
        }
    }
    return true;
}
int prime_factor(long long x){
    for(long long i=2; i<=sqrt(x); i++){
        if(x%i==0){
            return i;
        }
    }
}

int main() {
	// your code goes here
	long long t;
	cin >> t;
	vector<long long> A;
	map<int, int>B;
	A.push_back(2);
	A.push_back(3);
	for(long long i=4; i<1000005; i++){
	    if(isPrime(i)){
	        A.push_back(i);
	    }
	}
	long long temp=0;
	for(auto i:A){
	    temp+=i;
	    B[i]=temp;
	}
// 	for(auto i:A){
// 	    cout << i << " ";
// 	}
	while(t--){
	    long long k;
	    cin >> k;
	    long long ans=0;
        if(k%2==0){
            ans+=k*2;
        }
        else{
            if(isPrime(k)){
                ans+= B[k]*k;
            }
            else{
                ans+= B[prime_factor(k)]*k;
            }
        }
        cout << ans <<"\n";
	}

}

@hritik_02
plzz refer the following solution
https://www.codechef.com/viewsolution/1066070997

1 Like