#include <iostream>
using namespace std;
unsigned long long divsum(unsigned long long x);
int main()
{
unsigned long long x;
cin >> x;
unsigned long long y[200000];
for (unsigned int i = 0; i < x; i++)
cin >> y[i];
for (unsigned int i = 0; i < x; i++){
cout << divsum(y[i]) << endl;
}
return 0;
}
unsigned long long divsum(unsigned long long x){
int sum = 0;
for(unsigned int i = 1; i <= x/2; i++){
if(x % i == 0)
sum += i;
}
return sum;
}
I'm doing an online exercise and it says there are possible 2000000 cases in the first line, so I made an array of that amount, however, when I submit the solution it exceeds the time.. so I was wondering what is an alternative and faster way to do this? The program works fine right now, except it exceeds the time limit of the website.
You can allocate the array dynamically, so it will work better in cases where x < 200000
int main()
{
unsigned long long x;
cin >> x;
unsigned long long *y = new unsigned long long[x];
// you can check if new didn't throw an exception here
for (unsigned int i = 0; i < x; i++)
cin >> y[i];
for (unsigned int i = 0; i < x; i++){
cout << divsum(y[i]) << endl;
}
delete[] y;
return 0;
}
Since you know the size of the array, try vector and reserve.
int main()
{
unsigned long long x;
cin >> x;
unsigned long long var;
vector<unsigned long long> y;
y.reserve(x);
for (unsigned int i = 0; i < x; i++){
cin >> y[i];
}for (unsigned int i = 0; i < x; i++){
cout << divsum(var) << endl;
}
return 0;
}
And deal in const &
const unsigned long long & divsum(const unsigned long long & x){
int sum = 0;
unsigned long long x2 = x/2
for(unsigned int i = 1; i <= x2; i++){
if(x % i == 0)
sum += i;
}
return sum;
}
I think your assignment is more complex than you think. Your divsum(x) function should return sum of all divisors of x, right? In this case it's better to factorize the x, and calculate this sum using all the prime numbers (with powers), product of which is equal to the x. Take a look at:
http://en.wikipedia.org/wiki/Divisor_function
There are methods for recursive factorization - for example, if you have factorized all numbers until n, you can quickly find factorization for (n + 1). You also need to generate primes, Erathosphene sieve for first 2000000 numbers would be fine.
Related
I attempted a Hackerearth tutorial question and was able to solve it correctly using following code:
#include <iostream>
using namespace std;
int main() {
const long int MOD = 1000000007;
const long int SIZE = 100001;
long int t, n;
long int cache[SIZE];
cache[0] = 1;
for (long int i = 1; i < SIZE; i++) {
cache[i] = ( i * cache[i-1] ) % MOD;
}
cin >> t;
while (t--) {
cin >> n;
cout << cache[n] << endl;
}
return 0;
}
However when using scientific notation to write MOD or SIZE, online judge reports incorrect answers. What am I missing here?
#include <iostream>
using namespace std;
int main() {
const long int MOD = 10e9+7;
const long int SIZE = 100001;
long int t, n;
long int cache[SIZE];
cache[0] = 1;
for (long int i = 1; i < SIZE; i++) {
cache[i] = ( i * cache[i-1] ) % MOD;
}
cin >> t;
while (t--) {
cin >> n;
cout << cache[n] << endl;
}
return 0;
}
Scientific notation means “constant multiplied by ten to the power of x”. If you want 1000000007 you need 1e9, meaning “one followed by nine zeroes.” Now you use 10e9 which is “ten followed by nine zeroes” or “one followed by ten zeroes” so you’re off by a factor of ten.
I want to generate random test cases for my program, but it crashes after running 1 or 2 times.
I have used rand() function to generate random numbers for random test cases
but it is not running after one or sometimes two times.. and does not generate any random number. The program simply exits.
#include<bits/stdc++.h>
#include<ctime>
#include <time.h>
using namespace std;
long long int naive(long long int arr[],long long int n){
long long int max=-1;
for(long long int i=0;i<n;i++)
{
for(long long int j=0;j<n;j++){
if(arr[i]%arr[j] > max){
max = arr[i]%arr[j];
}
}
}
return max;
}
long long int efficent(long long int arr[],long long int n){
long long int max1=0,max2=0;
for(long long int i=0;i<n;i++){
if (arr[i] > max1)
{
max2 = max1;
max1 = arr[i];
}
else if (arr[i] > max2 && arr[i] != max1)
max2 = arr[i];
}
return max2%max1;
}
int main(){
srand(time(0));
long long int count=0;
int t=10;
while(t--){
long long int n;
n = rand()%10;
long long int arr[n];
for(long long int i=0;i<n;i++){
arr[i] = rand()%10;
}
long long int a,b;
a = naive(arr,n);
b = efficent(arr,n);
if(a == b)
cout<<"Naive : "<<a<<"\tEfficent : "<<b<<"\n";
else{
cout<<"\nNot Equal."<<"\nCount : "<<++count<<endl;
cout<<"Naive : "<<a<<"\tEfficent : "<<b<<"\n";
}
}
return 0;
}
In addition to the memory leaks and not declaring a variable sized array correctly mentioned in the other answer, the issue is that you are performing the mod operation on values that could be 0. This will force your program to exit. To fix this, change
arr[i] = rand()%10;
to something like
arr[i] = rand()%10+1;
to prevent division by 0.
EDIT: as mentioned by #Michael Dorgan, you should probably do the same for n. Change
n = rand()%10;
to
n = rand()%10+1;
to prevent 0 length arrays from being allocated.
This code is problematic:
while(t--){
long long int n;
n = rand()%10;
long long int arr[n];
for(long long int i=0;i<n;i++){
arr[i] = rand()%10;
}
If you need a variable size array, you should use long long int * arr = new long long int[n]; and delete[] arr; before the last closing brace of the while block.
I did implement Sieve prime generator. The code is pretty fast and consumes less memory. https://www.spoj.com/problems/PRIME1/
But I get "Wrong Answer" when I submit the solution. People online seem to just make the set the max to 32000 and run the solution. I don't exactly get where am I actually wrong? Or Is it just an extra newline (if possible) that makes the solution to be incorrect?
#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
unordered_map<long long int, bool> notPrime;
notPrime[0] = true;
notPrime[1] = true;
for (long long int x = 2; x < 100000; x++) {
if (notPrime[x]) continue;
for (long long int u = 2 * x; u < 100000; u += x)
notPrime[u] = true;
}
int n;
cin >> n;
while (n--) {
long long int s, e;
cin >> s >> e;
if (s < 0)
s = 0;
for (long long int i = s; i <= e; i++) {
if (!notPrime[i]) {
cout << i << '\n';
}
}
if (n)
puts("");
}
return 0;
}
I have searched google but I was unable to find the solution to my problem.
Here is my code-
#include <bits/stdc++.h>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
using namespace std;
long long buyMaximumProducts(int n, long k, vector <int> a) {
// Complete this function
vector<pair<int, int>> p;
long i;
for(i=0; i<n; i++) {
p.push_back(make_pair(a[i], i+1));
}
sort(p.begin(), p.end());
if(k < p[0].first)
return 0;
long long sum=0,stocks=0;
for(i=0;i<n;i++)
{
if((sum+p[i].first*p[i].second) <= k)
{
sum+=p[i].first*p[i].second;
stocks+=p[i].second;
}
else
break;
}
long long amtleft=k-sum;
**stocks+=(long long)(amtleft/p[i].first);**
return stocks;
}
int main() {
int n;
cin >> n;
vector<int> arr(n);
for(int arr_i = 0; arr_i < n; arr_i++){
cin >> arr[arr_i];
}
long long k;
cin >> k;
long long result = buyMaximumProducts(n, k, arr);
cout << result << endl;
return 0;
}
I'm getting floating point exception. I think the error is coming because of the star statement. Can anyone please tell me what could be the plausible reason and how to remove it?
The program contains at least 3 fault.
long long k;
cin >> k;
long long result = buyMaximumProducts(n, k, arr);
long long buyMaximumProducts(int n, long k, vector <int> a) {
k is 'long long' but parameter k is only 'long'.
for(i=0;i<n;i++) {
if((sum+p[i].first*p[i].second) <= k) {
sum+=p[i].first*p[i].second;
stocks+=p[i].second;
} else
break;
}
if we never get to the 'break' then 'i' is not valid for
stocks+=(long long)(amtleft/p[i].first);
causing an exception.
and if
p[i].first
is zero you get an divide by zero exception.
I am getting segmentation fault during my code and I was wondering why. I am taking a guess its something with the vector.erase() function.
#include <iostream>
#include <vector>
#include <math.h>
long long gcd(long long x, long long y);
std::vector<long long> factor(long long x);
typedef std::vector<long long> vectorNum;
int main(int argc, const char * argv[])
{
// insert code here...
long long unFriendly;
long long friendly;
long long counter = 0;
std::cin >> unFriendly;
std::cin >> friendly;
vectorNum n;
n = factor(friendly);
for(long long x = 0;x < unFriendly;x++){
long long num = 0;
std::cin >> num;
counter = gcd(friendly, num);
for(long long y = n.size() - 1;y >= 0;y--){
vectorNum temp(n);
if(counter % n.at(y) == 0){
n.erase(n.begin() + y);
}
}
}
std::cout<<n.size();
n.clear();
}
long long gcd(long long x, long long y){
while(y != 0){
long long a = x % y;
x = y;
y = a;
}
return x;
}
std::vector<long long> factor(long long x){
long long y;
long long root = sqrt(x);
std::vector<long long> vectorSet;
for(y = 2; y <= root;y++){
if(x % y == 0){
vectorSet.push_back(y);
vectorSet.push_back(x / y);
}
}
vectorSet.push_back(x);
return vectorSet;
}
Any kinds of insight would be perfect! Thanks in advance.
This is both the functions that are being called. Both of the gcd() and the factors() function as requested.
for(long long x = 0;x < unFriendly;x++){
long long num = 0;
std::cin >> num;
counter = gcd(friendly, num);
for(long long y = n.size() - 1;y >= 0;y--) { // <-- n.size() == 0, 0U - 1 is huge
vectorNum temp(n);
if(counter % n.at(y) == 0){ // <-- y is huge, throws std::out_of_range
n.erase(n.begin() + y);
}
}
}
I would strongly recommend running your code under a debugger. The Visual Studio debugger when running your code pretty quickly displayed this:
Your program will crash if friendly is 0. This is because the factor function will push 0 to the return vector. Inside your inner most loop, accessing n.at(y) will give the value 0 and dividing counter by 0 generates an exception that segfaults your application.