Multiple of 3 | CodeChef

I am not getting any understanding of this problem.
Please help me to figure out what logic it’s using.

Are you asking the logic to the solution or you’re not understanding the question?

logic

Tell us what you understand by reading the problem first?

Buddy i am able to understand the problem or may be i am wrong but my explanation is:

We have given TWO digits of a number whose length is N.
We have to find all of digits and check if the number is divisible by 3 or not.
(NUM%3==0)

for finding other digits we are given a formula so by following this we can calculate other digits but my approach is o(n) which is time taking approach so … the o(1) approach is good .
Also i am not getting exactly how the solution giving wright output?

Can you format your code, it’s messed up due to the forum software.

formated

I also have the same problem. I’m getting TLE error.
Please anyone explain me more efficient approach. I even tried reading solution of others but didn’t get it…

CODE :

#include
using namespace std;

int main() {
int t;
cin>>t;

while(t--)
{
    long long int k;
    int a,b, raw, moded;
    
    cin>>k>>a>>b;
    int total = a+b;
    
    if(total == 10 || total == 5)
    cout<<"NO"<<endl;
    
    else
    {
	    for(long long int i=2; i<k; i++)
	    {
	        raw = a+b;
	        moded = raw % 10;
	        total += moded;
	        a = raw;
	        b = moded;
	    }
	    
	    if(total % 3 == 0)
	    cout<<"YES"<<endl;
	    else
	    cout<<"NO"<<endl;
    }
}
return 0;

}

If it was brute force …like you did ,it would’nt be as interesting.

What you have to do is find some kind of pattern which occurs in the sequence di
and from that you should try the solution.
I would suggest that you try some examples of your own and you might as well find the pattern.

My code. Its a little shabby ,but if it helps…

1 Like

Hey,

I just saw the problem and got AC. Look at the example you will get to know that numbers are getting repeated so you only have to calculate the sum of numbers that are getting repeated and find a solution.

For more visit My Solution (Python 3)

Wishing you best of luck!

i basically did the same code but i am getting a TLE!!!

Can you paste your code in comment section, I am unable to access your solution page.

Can you paste your code in comment section, I am denied access to your solution page.

t=int(input())
def mul3(k,x,y):
total=str(x)+str(y)
raw=x+y
moded=raw%10
total+=str(moded)
x=raw
y=moded
if int(total)%3==0:
return ‘YES’
else:
return ‘NO’
for i in range(t):
k,x,y=map(int,input().split())
print(mul3(k,x,y))

Custom output for this snippet is coming right .But, its not accepted by the online judge. “Wrong Answer” it says. Maybe there is a silly mistake I can’t notice. Can anyone help me with this problem.

https://www.codechef.com/viewsolution/45368294