CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • All Contests
    • May Cook-Off 2013
    • May Challenge 2013
    • April Cook-Off 2013
    • April Challenge 2013
  • DISCUSS
    • Forums
    • Blog
    • Wiki
    • Facebook
    • Twitter
  • COMMUNITY
    • Campus Chapters
    • Host your Contest
    • Go for Gold
    • All Educational Initiatives
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Tutorials
    • Long Contest Ranks
    • Short Contest Ranks
    • Event Calendar
    • Top Contributors on Discuss
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Compete » August Challenge 2012 » Lucky Driving

Lucky Driving

Problem code: LUKYDRIV

  • All Submissions

All submissions for this problem are available.

"Nine is considered a good number in Chinese culture because it sounds the same as the word "long-lasting". Hence many people want their car registration numbers to sum up to 9 on adding the digits recursively.

A car registration number can consist of 1,2,3 or 4 digits. Examples of some good car numbers are 63, 018, 9099, etc. whereas 12,129 are not good.

Why is 9099 good?

9+0+9+9 = 27

2+7 = 9

Similarly it is not difficult to see why 63, 018, 6669, 9999… are good."

Given a string of digits, print how many subsequences of the string result in good car registration numbers.

Note:

The zeros at the starting of the number are to be accounted for, i.e. 018 and 18 are different numbers.

Input

First line of the input contains T (T <= 200) denoting the number of test cases. T lines follow each containing a non-empty single string S. S contains only digits i.e. from [0-9]. Length of S <= 10000.

Output

For each case print how many subsequences of the string result in good car registration numbers. As the answer can be quite large print it modulo 1000000007.

Example

Input

2
10292
0189

Output

2
6

Author: kaushik_iska
Tester: laycurse
Editorial http://discuss.codechef.com/problems/LUKYDRIV
Date Added: 21-04-2012
Time Limit: 2 sec
Source Limit: 50000 Bytes
Languages: ADA, ASM, BASH, BF, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.0.0-8, CPP 4.3.2, CS2, D, ERL, FORT, FS, GO, HASK, ICK, ICON, JAR, JAVA, JS, LISP clisp, LISP sbcl, LUA, NEM, NICE, NODEJS, PAS fpc, PAS gpc, PERL, PERL6, PHP, PIKE, PRLG, PYTH, PYTH 3.1.2, RUBY, SCALA, SCM guile, SCM qobi, ST, TCL, TEXT, WSPC


  • Submit

Comments

  • Login or Register to post a comment.

Can you clarify subsequences,

bcurcio @ 1 Aug 2012 04:09 PM
Can you clarify subsequences, and what you consider different subsequences? For example the answer for 919 is 3, right?

when you say "how many

piyushkumar @ 1 Aug 2012 04:37 PM
when you say "how many subsequences of the string result in good car registration numbers", that means how many "how many distinct subsequences are good", or repetition is allowed? Eg: for S="009", are "09","09","009","9" the good substrings, or just "09","009","9" ??

Definition of "subsequences"

tungnk1993 @ 1 Aug 2012 06:26 PM
Definition of "subsequences" & "different" plz :)

what if sequence has repeated

ashshetty333 @ 1 Aug 2012 06:33 PM
what if sequence has repeated digits, is it considered different. For eg : 909 wat is the answer 4 or 6

@ashshetty333, '90' is not a

yeputons @ 1 Aug 2012 06:40 PM
@ashshetty333, '90' is not a valid subsequence, because '9' goes after '0' in '10292'

I suspect he means pairwise

tomazos @ 1 Aug 2012 06:43 PM
I suspect he means pairwise distinct subsequences, where equality is string equality. So 919 is 2 and 909 is 4. But Im not sure.

919 has to be 4, otherwise

kuruma @ 1 Aug 2012 06:56 PM
919 has to be 4, otherwise the 1st test case would be wrong.

Subsequences do not have to

hiroto_adm @ 1 Aug 2012 07:01 PM
Subsequences do not have to distinct. So 919 has 7 subsequences 9, 1, 9, 91, 99, 19, 919. And there are 3 good subsequences 9, 9, 99.

@ashshetty333 909 is 6,not 4

darkgt @ 1 Aug 2012 09:26 PM
@ashshetty333 909 is 6,not 4

in this 0189 their are six

gaurav_singal @ 1 Aug 2012 10:35 PM
in this 0189 their are six subsequence 09,9,018,081,18,81...............

Is '0' a valid car

nihalpi1 @ 1 Aug 2012 10:49 PM
Is '0' a valid car registration number?

@gaurav_singal: 081 is not a

tomazos @ 1 Aug 2012 10:55 PM
@gaurav_singal: 081 is not a subsequence of 0189

@nihalpi1: '0' is valid but

tomazos @ 1 Aug 2012 10:56 PM
@nihalpi1: '0' is valid but not good. This can be verified via analzing the example input and output.

2 Admins : is answer for "99"

cupidon4uk @ 2 Aug 2012 02:23 AM
2 Admins : is answer for "99" is 2 ("9","9") or just 1 ? Thanks.

@cupidon4uk: answer is 3, man

qwaker00 @ 2 Aug 2012 02:44 AM
@cupidon4uk: answer is 3, man :)

ans for 10292 is 2 by: "09,

stalin @ 2 Aug 2012 02:57 AM
ans for 10292 is 2 by: "09, 9"(probably 90 not taken into account) but then how 0189 makes 6?: "018, 18, 09, 9".. it should be 4!! This means "90, 91" are also taken into account. Could someone explain?!

@admin please look at this

neil_812 @ 2 Aug 2012 04:09 AM
@admin please look at this solution http://www.codechef.com/viewsolution/1206184. It keeps getting WA but I've tried absolutely everything and I'm 100% sure it's correct (at least on my computer it is). The algorithm is definitely correct, and it's short and simple so there's clearly no bugs. So what the heck is going on??? I realize that I'm supposed to figure it out myself during the contest, but after the July cookoff when the exact same code got AC for betlista but RE for me (see http://www.codechef.com/viewsolution/1187366 and http://www.codechef.com/viewsolution/1191924), I have reason to believe that there are problems with the codechef judge. So could you please confirm that my solution really is wrong?

@stalin u r not taking 189 &

salmanblues @ 2 Aug 2012 07:18 AM
@stalin u r not taking 189 & 0189 ;)

So, the subsequences do not

alvarobsp @ 2 Aug 2012 12:06 PM
So, the subsequences do not need to be consecutive? if I have a number abcde, ace is a valid subsequence?

The subsequences do not have

hiroto_adm @ 2 Aug 2012 12:42 PM
The subsequences do not have to consecutive.

is there any change in

n2n_ @ 2 Aug 2012 04:29 PM
is there any change in codechef judge? earlier I used to get emails with the results of the judge, but I am no longer getting them now.

+1 to neil_812: getting

aamaly @ 2 Aug 2012 05:45 PM
+1 to neil_812: getting WA. Problem looks as very simple, but have so small number of correct solutions, it appears that it is checked incorrectly, please, recheck it for, say, submission 1209853.

guys i'm getting the answer.

remogoku @ 2 Aug 2012 06:09 PM
guys i'm getting the answer. but what that print it modulo 1000000007. what should i do..?? can any1 explain what that means modulo 1000000007..

@remogoku if answer of the

tornike4 @ 2 Aug 2012 06:27 PM
@remogoku if answer of the test is x, you mustnt write x , because its quite large , you must write x%1000000007. for example 17 % 6 = 5. 6 % 4=2 . 18 %5 =3

@tornike4: thanks dude.. :)

remogoku @ 2 Aug 2012 07:19 PM
@tornike4: thanks dude.. :) wat to do if get ans which is less than 1000000007 in some cases..?? should i've to print it directly.. i know the modulo operation. but still i'm really getting trouble with this modulo 1000000007 in every contest. just not getting it properly..

@remogoku if x less than

zentropy @ 2 Aug 2012 07:36 PM
@remogoku if x less than 1000000007,then print x,this is defined by modulo operation

what is the correct answer

lashabuxo @ 2 Aug 2012 07:59 PM
what is the correct answer for test 1 99 please answers me

for the string 999, should

manasvi2001 @ 2 Aug 2012 08:41 PM
for the string 999, should the answer be 7 ("9,9,9,99,99,99,999")?

is the answer for 666666:

rishabhnigam31 @ 2 Aug 2012 10:20 PM
is the answer for 666666: 21

the time limit is for all

abc_1988_1106 @ 3 Aug 2012 06:16 AM
the time limit is for all cases, or for one case?

I think the answer for 666666

fahim_xubayer @ 3 Aug 2012 01:54 PM
I think the answer for 666666 is 20 :| None of '6', '66', '6666' is lucky. '666' is lucky. There are six 6's in 666666 and there are 20 ways ( combinations ) to make a set of 3 things from 6 possible things. I might be wrong though :)

@fahim_xubayer : u r wrong ..

phantom11 @ 3 Aug 2012 03:03 PM
@fahim_xubayer : u r wrong .. .what abt all 6's

@phantom11 -- can a car

sakuag333 @ 3 Aug 2012 03:28 PM
@phantom11 -- can a car registration number be greater than 4 digits ??? in question it is clearly mentioned that "A car registration number can consist of 1,2,3 or 4 digits."

@skauag333 : thank you very

phantom11 @ 3 Aug 2012 05:34 PM
@skauag333 : thank you very much .. I was helpless for the last 2 days on reducing the complexity of my algorithm and now i think it is possible :)

@admin- WHEN WILL YOU

shakti11094 @ 3 Aug 2012 07:32 PM
@admin- WHEN WILL YOU ANSWER THESE ABOVE QUESTIONS AND CLEAR OUR DOUBTS ???? ARE YOU EVEN CONCERNED ???

somebody,plz xlpain me how

prasad_iiit @ 3 Aug 2012 08:28 PM
somebody,plz xlpain me how 0189 is 6? {9,18,018,189,0189} only 5 are coming na...

@admin nevermind, I found the

neil_812 @ 3 Aug 2012 08:46 PM
@admin nevermind, I found the problem.

@prasad_iiit : 09 ?? :P

phantom11 @ 3 Aug 2012 09:31 PM
@prasad_iiit : 09 ?? :P

@phantom11: I wonder is there

tyrant @ 3 Aug 2012 09:58 PM
@phantom11: I wonder is there any special corner case that we should take into account? I implemented a brute-force method to check with my solution, and I haven't found one that doesn't match. Unfortunately, I keep getting WA for some reasons, so my best guess would be some very trick cases might exists. For example with this string "99991812313131414141", my answer is 561. Could you help me verify this? Thanks.

does S<=10000 means that S

stalin @ 3 Aug 2012 10:37 PM
does S<=10000 means that S can contain atmost "10,000" digits?!

@tyrant 116255

tutum @ 3 Aug 2012 11:47 PM
@tyrant 116255

@tutum: You meant the answer

tyrant @ 4 Aug 2012 12:08 AM
@tutum: You meant the answer is for 99991812313131414141 is 116255?

That's what I thought at

neil_812 @ 4 Aug 2012 12:22 AM
That's what I thought at first too, but the answer is actually 561. @tutum read the problem carefully...

@neil_812: Thanks for the

tyrant @ 4 Aug 2012 12:34 AM
@neil_812: Thanks for the verification. I believe my solution works for 99% of the test cases since I've been testing with more than 1000 inputs (compared with brute-force solution), still got WA. Just can't see where I missed :(

Finally got AC, one stupid

tyrant @ 4 Aug 2012 12:58 AM
Finally got AC, one stupid typo caused me a day :(

2 sec. is time limit for 200

cs5090240 @ 4 Aug 2012 01:13 AM
2 sec. is time limit for 200 test cases or just 1 test case ?

There are several the judge

hiroto_adm @ 4 Aug 2012 01:20 AM
There are several the judge input files, and each of them can have 200 test cases. Your code must run for each judge input file within the time limit 2 sec.

@tyrant: could you please

stalin @ 4 Aug 2012 01:39 AM
@tyrant: could you please explain how the ans is 561 for 99991812313131414141?

@stalin: discussing algorithm

tyrant @ 4 Aug 2012 01:42 AM
@stalin: discussing algorithm during contest is not allowed. Sorry! Hint: "A car registration number can consist of 1,2,3 or 4 digits".

@tyrant: I got it! thnx :)..

stalin @ 4 Aug 2012 02:06 AM
@tyrant: I got it! thnx :)..

what if sequence is 00099?

cobra @ 4 Aug 2012 05:28 PM
what if sequence is 00099? Are these subsequences "09","09","09","09","09","09","9","9","99","099","099","099","0099","0099","0099","00099" good for sequence 00099?

how to guarantee tha an

masterjichef @ 4 Aug 2012 07:53 PM
how to guarantee tha an overflow wont occur?? if we take long long int, then mod 1000000007, at each step, does this ensure that no overflow occurs??

@admin: Is the subsequence

squeezethekey @ 5 Aug 2012 02:17 AM
@admin: Is the subsequence continuous, i.e. the digits of subsequence adjacent to each other.

@admin... plz reply... i too

nitish712 @ 5 Aug 2012 11:06 AM
@admin... plz reply... i too had the same doubt as @cobra's....

My algorithm performs less

cs5090240 @ 5 Aug 2012 12:49 PM
My algorithm performs less than 10^8 operations per 200 test cases and is till getting TLE. Any peculiar reason why?

My algo takes o(n) still time

ezio_123 @ 5 Aug 2012 01:49 PM
My algo takes o(n) still time limit exceeded .

finally Got Accepted

ezio_123 @ 5 Aug 2012 01:51 PM
finally Got Accepted

language partiality

hellcoderz @ 5 Aug 2012 08:32 PM
language partiality

Please reply

squeezethekey @ 6 Aug 2012 01:24 PM
Please reply

for the example input

ravinder_singh @ 6 Aug 2012 06:03 PM
for the example input 0189: 0,1,8,9,01,18,89,018,189,0189 Then good sub sequences are 9,18,018,189,0189 Which is 5. Then how the output is 6?

what is the proper order for

mehrdad_ap @ 6 Aug 2012 06:19 PM
what is the proper order for this problem ? my solution works in O(N*9*4) , where N is size of string, but it's TLE ! i'm so confused , can anyone say something about the time-complexity of his AC solution ?

@ravinder_singh , you forgot

mehrdad_ap @ 6 Aug 2012 06:21 PM
@ravinder_singh , you forgot 09 ;)

i'm using long long int array

mehrdad_ap @ 6 Aug 2012 07:04 PM
i'm using long long int array , can it be a reason for TLE ?!!

@mehrdad_ap : Same case with

mng88 @ 6 Aug 2012 07:50 PM
@mehrdad_ap : Same case with me.. My solution with same order as yours is getting TLE. Even on using an int array instead of a long long int, and using subtraction instead of mod operation gives a TLE. No more optimization looks possible with this approach. Seems like some better approach is needed..

@mng88 : however it's an

mehrdad_ap @ 6 Aug 2012 08:09 PM
@mng88 : however it's an unreasonable TLE error for this problem :(

Time limit is too strict...my

shrknt @ 7 Aug 2012 12:25 AM
Time limit is too strict...my soln too is O(n) but times out

AC at last.. Had to change

mng88 @ 7 Aug 2012 12:38 AM
AC at last.. Had to change the approach completely..

@admin: I am getting

guptaanil2k1 @ 7 Aug 2012 10:52 PM
@admin: I am getting compilation error but no error message in displayed. http://www.codechef.com/view/error/1232491.

@admin i m getiing

shrikant_p @ 7 Aug 2012 11:48 PM
@admin i m getiing compilation error without any message but the same solution without any change is giving TLE on next try ,i think there must be some problem ,i got 3 compilation errors because of this problem

@admin:: i m also getting

rajneal14 @ 8 Aug 2012 12:16 AM
@admin:: i m also getting complilation error and no messge is being displayed,and program is working all fine on my machin(dev c++)..:(...plese do check it,or inform us about the error ps......i was expecting TLE..:)

@mehrdad_ap : I dont think

malice @ 8 Aug 2012 12:28 AM
@mehrdad_ap : I dont think there's an algorithm that'll do this in O(n) , it should be atleast have a power of n , you cannot find all numbers scanning the aaray just one time

I am getting compilation

yerzhanu @ 8 Aug 2012 01:40 AM
I am getting compilation error and log doesn't show anything. The program compiles good at server then I add some operation after that I had compilation error. After I resubmited my wrong answer solution I had compilation error. What is going on with server?

@yerzhanu: I have similar

betlista @ 8 Aug 2012 12:10 PM
@yerzhanu: I have similar problem when I upload the file, when copy&paste the code to editor it's ok, try it ;-)

my code is giving correct

nalinraj1093 @ 8 Aug 2012 06:48 PM
my code is giving correct solution 4 all test cases i have tried...bt wrong ans:( is there any tricky test case ....pls help......!!!1

0189 :-> 18, 81, 018, 108,

bijupv @ 10 Aug 2012 07:10 PM
0189 :-> 18, 81, 018, 108, 180 0189, 1089, 1809,1890..... i think, for the 2nd input answer is not 6...is there anything wrong with my understanding ?

@bijupv: Have you tried

gennady_adm @ 10 Aug 2012 07:43 PM
@bijupv: Have you tried reading other comments on this page before posting? The answer to your question is: 81 is not a subsequence of 0189, because 8 comes after 1 in 0189. Neither of 108, 180, 1089, 1809 and 1890 is a subsequence of 0189 as well. Good subsequences of 0189 are 9, 09, 18, 018, 189 and 0189.

gOt acc

saurabheights @ 11 Aug 2012 08:16 AM
gOt acc

unistd.h ,....guys are using

deepakmishra12 @ 15 Aug 2012 12:52 AM
unistd.h ,....guys are using .............dangerous coders.......

SUCCESSFUL SUBMISSIONS


Fetching successful submissions

CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of algorithms, computer programming and programming contests. At CodeChef we work hard to revive the geek in you by hosting a programming contest at the start of the month and another smaller programming challenge in the middle of the month. We also aim to have training sessions and discussions related to algorithms, binary search, technicalities like array size and the likes. Apart from providing a platform for programming competitions, CodeChef also has various algorithm tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our programming contest judge accepts solutions in over 35+ programming languages. Preparing for coding contests were never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming challenges that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly coding contest and the shorter format Cook-off coding contest. Put yourself up for recognition and win great prizes. Our programming contests have prizes worth up to Rs.20,000 and $700lots more CodeChef goodies up for grabs.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be a part of CodeChef's Forums and interact with all our programmers - they love helping out other programmers and sharing their ideas. Have discussions around binary search, array size, branch-and-bound, Dijkstra's algorithm, Encryption algorithm and more by visiting the CodeChef Forums and Wiki section.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. You can also host a coding contest for your institute on CodeChef, organize an algorithm event and be a guest author on our blog.

Go For Gold

The Go for Gold Initiative was launched about a year after CodeChef was incepted, to help prepare Indian students for the ACM ICPC World Finals competition. In the run up to the ACM ICPC competition, the Go for Gold initiative uses CodeChef as a platform to train students for the ACM ICPC competition via multiple warm up contests. As an added incentive the Go for Gold initiative is also offering over Rs.8 lacs to the Indian team that beats the 29th position at the ACM ICPC world finals. Find out more about the Go for Gold and the ACM ICPC competition here.