LUCKY5 - Editorial

PROBLEM LINKS

Practice
Contest

DIFFICULTY

EASY

EXPLANATION

In this problem the only thing you should notice is that the only operation that you need to use is the changing of digits.

Indeed, adding leading digit operation worsens our situation - it gives nothing good.
On the other hand, incrementing number by 1 is equivalent to changing of last digit except the case when the last digit is 9. If N has k trailing nines and d < 9 is the next digit after nines then the block d 9 9 … 9 9 will be changed to (d+1) 0 0 … 0 0. So it is equivalent to changing of digit d and some useless changes from 9 to 0 that do not involve lucky digits. So we can use just one change of digit instead of this incrementing operation with the same effect in sense of lucky digits.
After noticing this, we see that the answer to the problem is simply the number of unlucky digits of N.

SETTER’S SOLUTION

Can be found here.

TESTER’S SOLUTION

Can be found here.

2 Likes

This is incorrect.
Suppose N = 539, add leading digit operation gives 544, then change 5 to 4 => total number of operations is 2 while number of unlucky digits is 3.

What is wrong in my solution?
Instead of using a String, I took character by character input.
Please have a look CodeChef: Practical coding for everyone

what if none of the input digit is 4 or 7 and length of input is 10^100000 ? In that case 10^10000 will be the answer .

5 Likes

Hello priyankpalod,you can check out this solution check if still problem persists.

question statement has dual meaning…add any leading digits to number

what is the error in my code

for _ in range(int(input())):

x=input()

a=x.count(‘4’)+x.count(‘7’)

print(int(len(x)-a))

1 Like

‘Add leading digit’ means write some digit at the beginning of the number like: 539 -> 4539.

1 Like

I think u havenot got the point adding 1 to 539 means total of operations as 1 add + change of two digits on adding and further we have to change units place afterwards…

Setter problem have some error in string function.
This is correct one.
string s;
int t;

int main()
{
cin >> t;
FOR (tt,0,t)
{
cin>>s;
int c = 0;
FOR (i,0,s.length())
if (s[i] != ‘4’ && s[i] != ‘7’)
c++;
cout << c << endl;
}

	return 0;

}

Use BigInteger in Java because N value can be very very big.

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

Whats wrong in my code, anybody?

n=int(input())
for i in range(1,n+1):
s=input()
s.strip()
sum=0
for i in s:
#print(i,end=" ")
if(i!=‘4’ and i!=‘t’):
sum+=1
print(sum)

this question has two meaning

  • Take some digit of N and replace it by any non-zero digit.
  • Add
    any non-zero leading digit to N .

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

whats wrong ,help ,bachao ! :sob:

  1. Contraint of N (ie 10^100000 0) tends to infinity.
  2. Problem setters should take care of proper explanation of operations given in question.
1 Like

for _ in range(int(input())):
n=input()
count=0
for i in n:
if(i!=‘4’ and i!=‘7’):
count=count+1
print(count)

what is error

You cannot take number input as integer , as range of input is very high