Subset Sum

QUESTION

Roy is very weak in mathematics. His mother gave him a problem and left to work. He finds it very difficult to find the soultion. Help Roy.\n\nGiven a set A which contains elements ranging from 1 to N.Find the sum of the elements in all possible subsets of the given set.\n\nInput Format:\n\nT, the number of test cases. Following T lines contain N, number of elements in set.\n\nOutput Format:\n\nT lines indicating the subset sum. print the answer modulo (109+7).\n\nConstraints: \n\n1<=T<=1000 1<=N<=2000.

ANSWER

#include<stdio.h>
int main()
{
   int t;
   scanf("%d",&t);
   while(t--)
  {
     int n,i;
     scanf("%d",&n);
     long long int ans=1;
     for(i=0;i<n-1;i++)
        ans=ans<<1;
     ans=ans*(n*(n+1))/2;
     printf("%lld\n",ans);
   }
   return 0;
}

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock