Skip to main content

finding sum:

finding sum: 

Problem Description

You are given a set of N positive integers and another integer P, where P is a small prime. You need to write a program to count the number of subsets of size (cardinality) 3, the sum of whose elements is divisible by P. Since the number K of such sets can be huge, output K modulo 10007 1009 (the remainder when K is divided by 1009)

Constraints

N <= 500
P<50
All integers <=1000

Input Format

First line two comma separated integers N, P
The next line contains N comma separated integers

Output

One integer giving the number of subsets the sum of whose elements is divisible by P. Give result modulo 1009


Explanation

Example 1
Input
4,5
5,10,15,20
Output
4
Explanation
Every non empty subset of the given numbers has sum of its elements a multiple of 5. Since there are 4 subsets of size 3, the output is 4.
Example 2
Input
5,5
3,7,12,13,15
Output
4
Explanation
There are 4 subsets of size 3 with sum a multiple of 5: {3, 7, 15}, {12, 13, 15}, {7, 13, 15}, {3, 12, 15}, Hence the output is 4.




 Solution 
#include<stdio.h>
 int main()
int n,i,j,k,sum=0,a[100],p,c=0; 
scanf("%d,%d\n",&n,&p); 
for(i=0;i<n;i++) 
 { 
 scanf("%d,",&a[i]);
 }
 for (i = 0; i < n; i++) 
 {
 for (j = i + 1; j < n;)
 {
 if (a[j] == a[i])
 { 
for (k = j; k < n; k++)
 {
 a[k] = a[k + 1]; } n--;
 }
 else j++;
 }
 }
 for(i=0;i<n;i++)
 { 
 for(j=i+1;j<n;j++)
 {
 for(k=j+1;k<n;k++)
 { 
sum=a[i]+a[j]+a[k];
 if(sum%p==0) c++;
 sum=0; 
 }
  }

 }
 printf("%d",c);
 return 0;
 }

Comments

Popular posts from this blog

JAVA program of showing the student marksheet

package june13; import java.util.Scanner; public class student { int roll; String name; String email; int hindi,english,physics,chemistry,maths; void  input_details(){ System.out.println("please enter the following details "); Scanner sc= new Scanner(System.in); System.out.println("enter the roll number "); roll=sc.nextInt(); System.out.println("enter the name of student "); name=sc.next(); System.out.println(" enter the email"); email=sc.next(); System.out.println(" enter the marks of hindi "); hindi=sc.nextInt(); System.out.println("enter the marks of english "); english=sc.nextInt(); System.out.println(" enter the marks of physics ");; physics=sc.nextInt(); System.out.println(" enter the marks of chemistry "); chemistry=sc.nextInt(); System.out.println("enter the marks of maths "); maths=sc.nextInt(); ...

Program of Making a simple calculator using java swing

import javax.swing.*; import java.awt.event.*; class Calc implements ActionListener {     JFrame f;     JTextField t;     JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,bdiv,bmul,bsub,badd,bdec,beq,bdel,bclr;     static double a=0,b=0,result=0;     static int operator=0;     Calc()     {         f=new JFrame("Calculator");         t=new JTextField();         b1=new JButton("1");         b2=new JButton("2");         b3=new JButton("3");         b4=new JButton("4");         b5=new JButton("5");         b6=new JButton("6");         b7=new JButton("7");         b8=new JButton("8");         b9=new JButton("9");         b0=new JButton("0");         b...

C Program to find next larger number from a given array from target number

  Here is Program:   #include<stdio.h> #include<stdlib.h>   void sort(int arr[] ,int n)  {   int i,j,temp;   for (i = 0 ; i < ( n - 1 ); i++)   {     for (j = i+1 ; j <( n - 1); j++)     {       if (arr[i] > arr[j])       {         temp  = arr[i];         arr[i]   = arr[j];         arr[j] = temp;       }      }    }   }   int main()   {     int a[] = {2,11,11,6,80};     int i, num1 = 8;     sort(a,5);         for(i=0;i<sizeof(a)/sizeof(a[i]);i++)     {         if(a[4]<num1)          {   ...