Reputation: 95
here is the code for the question named maxsub on spoj
#include<iostream>
#include<algorithm>
using namespace std;long long pow(int n);
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
long long int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
sort(arr,arr+n);
int j=n-1;
if(arr[n-1]<0)
{
while(arr[j]==arr[j-1])
{
j--;
}
cout<<arr[n-1]<<" "<<(n-j)<<endl;
j=n-1;
}
else if(arr[n-1]==0)
{
while(arr[j]==arr[j-1])
{
j--;
}
cout<<arr[n-1]<<" "<<(pow((n-j))-1)%1000000009<<endl;
j=n-1;
}
else{
long long int sum=0;
while(arr[j]>0){
sum+=arr[j];j--;
}
int k=0;
if(arr[j]==0)
{ k=1;
while(arr[j]==arr[j-1])
{
j--;k++;
}
}cout<<sum<<" "<<pow((k)%1000000009)<<endl;
}
}
}
long long pow(int n)
{ if(n==1)
{
return 2;
}
long long int m= pow(n/2);
if(n%2==0)
{
return (m*m)%1000000009;
}
else
{
return (m*m*2)%1000000009;
}
}
this code is giving sigsegv error but if i replace the upper pow function with this then it is working fine.
long long pow(int n)
{int i=1;long long sum=1;
while(n>=i)
{
sum=sum*2;
if(sum>=1000000009)
{
sum=sum%1000000009;
}i++;
}
return sum;
}
i used the recursive way to find the power just to reduce the runtime of solution. but i am not able to get the difference it is making while on ideone both are working fine.
Upvotes: 0
Views: 113