Help me in solving PMD7 problem

My issue

Why the value of M is only accepted for 0,1,2,3 and 4 in everyone’s code when the constraint is 0<=M<=100 in Problem code:PMD7 ??

My code

#include <bits/stdc++.h>
#include<algorithm>
using namespace std;

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--){
	    long x;
	    long y=0;
	    int m;
	    cin>>x>>m;
	      long a,b;
	    while(x!=0){
	        a=x%10;
	        b=pow(a,m);
	        b=b%10;
	        y=y*10+b;
	        x=x/10;
	    }
	    if(y%7==0){
	        cout<<"YES"<<endl;
	    }else{
	        cout<<"NO"<<endl;
	    }
	}
	return 0;
}

Problem Link: CodeChef: Practical coding for everyone

@akanksha2_code
U have to calculate pow(a,m) by looping till m and take %10 each time u multiply.

Can you please elaborate what you want to say?

U have to calculate the a^m like this .
int tm=1;
for(int i=0;i<m;i++)
{
tm=(tm*a)%10;
}

include <bits/stdc++.h>
include
using namespace std;

int main() {
// your code goes here
int t;
cin>>t;
while(t–){
long x;
long y=0;
int m;
cin>>x>>m;
long a,b;

    while(x!=0){
            a=x%10;
           int tm=1;
              for(int i=0;i<m;i++)
             {
                    tm=(tm*a)%10;
             }
     /*   b=pow(a,m);*/
     b=tm;
        b=b%10;
        y=y*10+b;
        x=x/10;
    }
    cout<<y<<endl;
    if(y%7==0){
        cout<<"YES"<<endl;
    }else{
        cout<<"NO"<<endl;
    }
}
return 0;

}

It still giving wrong answer on submission.

@akanksha2_code
This is my code .
Hope u will get it .
Let me know in case u get stuck at any point.

#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--)
	{
	    string x,s1;
	    cin>>x;
	    int m;
	    cin>>m;
	    for(int i=0;i<x.size();i++)
	    {
	        int val=x[i]-'0';
	        int tm=1;
	        for(int j=0;j<m;j++)
	        {
	            tm=(tm*val)%10;
	        }
	        s1+=(tm+'0');
	    }
	    reverse(s1.begin(),s1.end());
	    int an=stoi(s1);
	    if(an%7==0)
	    cout<<"YES";
	    else
	    cout<<"NO";
	    cout<<endl;
	}
	return 0;
}

Thank you so much for your help.
I have transformed my code according the logic of your code.
Below is my correct transformed code.

include <bits/stdc++.h>
include
using namespace std;

int main() {
// your code goes here
int t;
cin>>t;
while(t–){
long x;
long y=0;
int m;
cin>>x>>m;
long a;

    while(x!=0){
            a=x%10;
           int tm=1;
              for(int i=0;i<m;i++)
             {
                    tm=(tm*a)%10;
             }
        y=y*10+tm;
        x=x/10;
    }
   
    if(y%7==0){
        cout<<"YES"<<endl;
    }else{
        cout<<"NO"<<endl;
    }
}
return 0;

}

1 Like