PlayStation india

All related to gaming in india

LightBlog

Breaking

Sunday, 11 December 2022

Divisible by K codechef solution in c++ || December Long 2022 (Rated for Div 3 & 4)

 

Problem

You are given an array A consisting of N positive integers and a positive integer K.

Find whether there exists a subset S of the elements of A such that the product of all elements of S is divisible by K.

Note that a subset is obtained by deleting some or no elements without changing the order of the remaining elements.

Input Format

  • The first line of input will contain a single integer T, denoting the number of test cases.
  • Each test case consists of multiple lines of input.
    • The first line of each test case contains two space-separated integers N and K — the number of elements of A and the above mentioned integer K.
    • The second line of each test case contains N space-separated integers A_1,A_2,\ldots,A_N representing the array A.

Output Format

For each test case, print on a new line the answer: YES if there exists a subset S and NO otherwise.

Each character of the output may be printed in either uppercase or lowercase, i.e, the strings YesYES, yesyEs` will all be treated as identical.

solution of this queston --- Make sure to change the variable

#include<bits/stdc++.h>


using namespace std;

void solve()

{

    long long a,b;

    cin>>a>>b;

    long long c =1;

    long long ar[a];

    bool flag = false;

    for(int i=0; i<a; ++i)

    {

        cin>>ar[i];

    }

   

  

    for(int i=0; i<a; ++i)

    {

         c = (c*ar[i])%b;

    }

    if(c==0)

    {

        cout<<"YES"<<endl;

    }else{

        cout<<"NO"<<endl;

    }

  }


int main()

{

  int t;

  cin >>t;

  while(t--)

  {

  solve();

  }  

}

No comments:

Post a Comment