#include
#include<math.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t–){
int x,m,c=0,d=0,b=0,y=0;
cin>>x>>m;
c=x;
while(c>0){
d=c%10;
c/=10;
y=pow(d,m);
b=b*10+(y%10);
}
if(b%7==0)
cout<<“yes\n”;
else
cout<<“no\n”;
}
return 0;
}
Quick! You need to solve this problem in order to help the Chef get an A on a math exam! The problem is as follows:

You are given two integers: X and M. Now, you need to replace each digit of X (let’s call them di) with dMimod10. Let’s call the new number Y and reverse it (explained in note 3). Check whether Y is divisible by 7.

Note 1: dMi=di⋅di⋅…⋅di (M times) - definition of di to the power of M. Special case: dMi=1, when M=0
Note 2: Xmod10 is the remainder of division X by 10, where X is some integer number.

Note 3: Reversing an integer is an operation that reverse the order of its digits and erase leading zeros. For example: 123 when reversed becomes 321 and 450 when reversed becomes 54.

Input Format
The first line of the input contains a single integer: T - representing the number of test cases
Each of the test cases contains a line with two space-separated integers: X and M
Output Format
For each test case output a line containing either “YES” or “NO” depending on whether Y is divisible by 7.

You may print each character of the string in uppercase or lowercase (for example: the strings “yes”, “YeS”, “YES” will be treated as the same strings).

Constraints
1≤T≤500
1≤X≤109
0≤M≤100
Sample Input 1
1
123 2
Sample Output 1
NO
Explanation
After squaring all the digits the number we get is 149 and when we reverse it, it becomes 941. 941 is not divisible by 7.

Sample Input 2
1
123 4
Sample Output 2
YES
Explanation
1 to the power of 4 is 1. 2 to the power of 4 is 16mod10=6. 3 to the power of 4 modulo 10 is 1. So the number we get is 161 and it stays the same when reversed. 161 is divisible by 7.

1 Like

If this is a problem on some programming platform, do share the link.

Everything seems fine except

Are you aware of the functioning of the pow (*, *) being called ? What can be the max value of d^m ?

If the problem persists after handling that, please feel free to ask again.

1 Like

I was having the same issue and now I have fixed the part that you asked to fix. But I am still getting WA. Why?

I wrote the following code:

``````#include <bits/stdc++.h>

#define int long long
#define float long double
#define endl '\n'

using namespace std;

int getLastDigit (int a, int b)
{
int current = a;
for (int i = 1; i <= b - 1; ++i)
{
current = (current*a) % 10;
}
return current;
}

void solve()
{
int x, m;
cin >> x >> m;

int y = 0;
while (x)
{
y *= 10;
y += getLastDigit (x%10, m);
//y += (long long) pow (x%10, m) % 10;

x /= 10;
}
cout << (y%7 == 0 ? "YES" : "NO") << endl;
}

int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);

int t = 1;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
``````

P.S: It’s a problem called " Power M divisibility by 7" on Codechef.

Getlastdigit function will return current=a when m=0 , rather than current=1,i think that’s why you are getting wrong answer.
You can try
if(m=0)
Current =1
else
same code in getlastdigit function

1 Like

its running properly but test cases are failing due to some runtime pls help …last question left

import itertools
import numpy as np
d=[]
res=[]
f=[]
ctr=0
n=int (input())
for i in range (n):
tot=int(input())
x = list(map(int, input().split()))
for j in range (1,tot+1):
f=list(itertools.combinations(x,j))
l=len(f)
for k in range (l):
d.append(f[k])

``````#print (d)
l1=len(d)
#print(l1)
for k in range (l1):
t=len(d[k])
if t==1:
if d[k][0]==7:
ctr+=1
#print (ctr)
else :
sumx=0
for i in range(t):
sumx=(sumx*10)+d[k][i]
if sumx%7==0:
ctr+=1
res.append(ctr)
d=[]
ctr=0
``````

for i in range (n):
print(res[i])