[1,2] is a good subsequence since 1+2 is divisible by 3. The problem statement mentions a good subsequence if its sum is divisible by 3, and by not parting the subsequence into another subsequence as you have done by parting [1,2] into [1], [2], [1,2]. The original [1,2] is already a subsequence. Please correct me if I am wrong. Thank you!
I am getting partial correct answer with this can anyone tell me whats the issue is?
int main(void){
long int t;
cin>>t;
while(t–){//cout<<t<<"\n";
long long int n,m,x,cnt=0;
cin>>n>>m;
while(n–){
cin>>x;
if(x%m==0)cnt++;
}
cout<<(pow(2,cnt)-1)<<"\n";
When you are directly doing cout << pow(2, freq)-1 it will output it as it is which means pow(2, freq) is outputting answers in double format which can make answers wrong for bigger constraints. But when you are putting it in int sum = pow(2, freq)-1 it converts/casts itself to int and then outputting the ans will give AC even for bigger constraints.
If you directly want to output the answer then you have to do like this cout << (int)pow(2, freq)-1;
which is casting the double to int.
/* Name of the class has to be “Main” only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
try{
//Your Solve
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] res = new int[n];
for(int i=0;i<n;i++)
{
int a = sc.nextInt();
int[] t = new int[a];
int k = sc.nextInt();
for(int j=0;j<a;j++)
{
t[j] = sc.nextInt();
}
for(int i1=0;i1<t.length;i1++)
{
int count = 0;
int sum = t[i1];
if(sum%k == 0)
{
count++;
}
for(int j1=i1+1;j1<t.length;j1++)
{
sum = sum+t[j1];
if(sum%k == 0)
{
count++;
}
}
res[i1]=count;
}
}
for(int i=0;i<n;i++)
{
System.out.println(res[i]);
}
}catch(Exception e){
return;
}
}
}
this is my code i guess this will work please check this code and please say if i am wrong
If there are k candidate elements that can be used, we have two options for each of them- either we can include it or not and thus pow(2,k) and we subtract 1 for the case when we decide to not include all k elements.
In Good Sequence, sum of elements of every non-empty Subsequence should be divisible by m.
Consider seq=[2,4,3,6] and m=2. There are 3 elements divisible by m but one isn’t. So, I can give you some sub sequences of seq such that sum of elements of the sub sequence isn’t divisible by m. Examples of such sub sequences are [2,3], [3], [2,3,6] ... and more. So, we can prove if a sequence has atleast 1 element that isn’t divisible by m, the seq definitely isn’t a good one.
Now can we prove the reverse? Consider a seq having all elements divisible by m. Is it always a good one? Yes! If a \% m=0 and b \% m=0 , then (a+b) \% m=0.
Now Question asks how many Sub sequence of a given sequence are good. A Sub sequence of a given seq will be good iff it has all the elements divisible by m. Take all the elements of the seq divisible by m. and we can either put or not put it in the Sub sequence. seq=[2,4,3,6] and m=2 k=3
Candidate elements - [2,4,6] in order as they appear in the seq.