Skip to main content

Array Product Problem:

Array Product

Array Product Problem Description You are given a list of N integers and another positive integer K. Write a program to compute the number of ways in which the product P of the given N integers can be expressed as a product of K positive integers (not necessarily distinct). The order of the factors in the expression is not important. For example, 1 x 2 x 3 and 2 x 3 x 1 are not counted as different ways of expressing 6 as a product of three integers. Constraints The product of the N integers <= 10^9 Each of the N integers <=5000 Input Format Output One line containing the number of ways in which the product of the N integers can be expressed as a product of K positive integers Explanation Example 1

 Input 
 2 4
 2 3 
Output 
 2
 Explanation The product of the given integers is 6. This can be expressed as a product of 4 integers in 2 ways: 1x1x1x6, 1x1x2x3

 Example 2

 Input 
 2 3
 4 16
 Output
 7 Explanation The product is 64. This can be expressed as a product of three integers in the following ways: 1 x 1 x 64 1 x 2 x 32 1 x 4 x 16 1 x 8 x 8 2 x 2 x 16 2 x 4 x 8 4 x 4 x 4

Solution


#include <stdio.h>
 int c=0;
 void product(int k,int i,int m)
 {
int j;
 if(k==0)
 {
if(i<=m)
{
 c++;
 return;
 }
 }
 else

 {
 if(i>m)
 return ;

 for(j=i;j<=m;j++)
 {
if(m%j == 0)
 product(k-1,j,m/j);
 }
 }
 }
 int main()
 {
 int n,k,a[10],m=1,i,j;
 scanf("%d%d",&n,&k);
 for(i=0;i<n;i++)
 {
 scanf("%d",&a[i]);
 m=m*a[i];
 }
 for(i=1;i<=m;i++)
 {
 if(m%i==0)
 product(k-2,i,m/i);
 }
 printf("%d\n",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)          {   ...