For given number N(Normal number) you must output amount of N-digit numbers, such, that last digits of their square is equal to 987654321.
where 1<=N<=10^6
It may be simple combinatorics problem. I am not sure. I am trying to find algorithm for this problem. What is the best algorithm to solve this problem?
The question is:
Find the number of n-digit number x that satisfies x^2 mod 1000000000 = 987654321.
1. Solution for n <= 9
First, calculate the value of x that satisfies x^2 = 987654321, 0 <= x < 1000000000.
You can pre-count in your PC because you only have to check for 10^9 integers.
I calculated the value and the result was following:
Result: 111111111,119357639,380642361,388888889,611111111,619357639,880642361,888888889
Therefore, if n <= 8 the answer is zero, and if n = 9 the answer is 8.
2. Solution for n >= 10
You can prove this facts because x^2 mod 1000000000 is cycle with length 10^9.
There are 8 solutions in 0 <= x < 10^9.
There are 8 solutions in 10^9 <= x < 2*10^9.
There are 8 solutions in 2*10^9 <= x < 3*10^9.
There are 8 solutions in k*10^9 <= x < (k+1)*10^9 (k is an integer).
So, you can say these things.
There are 8 solutions in 0 <= x < 10^9.
There are 80 solutions in 0 <= x < 10^10.
There are 800 solutions in 0 <= x < 10^11.
Therefore,
There are 72 solutions in all 10-digit number.
There are 720 solutions in all 11-digit number.
There are 7200 solutions in all 12-digit number.
So, you only have to output "72" + (n-10 0's) if n > 9.
3. Conclusion
If n <= 8, the answer is 0.
If n = 9, the answer is 8.
If n >= 10, the answer is 72 * 10^(n-10).
Related
Problem
Isyana is given the number of visitors at her local theme park on N consecutive days. The number of visitors on the i-th day is Vi. A day is record breaking if it satisfies both of the following conditions:
The number of visitors on the day is strictly larger than the number of visitors on each of the previous days.
Either it is the last day, or the number of visitors on the day is strictly larger than the number of visitors on the following day.
Note that the very first day could be a record breaking day!
Please help Isyana find out the number of record breaking days.
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each test case begins with a line containing the integer N. The second line contains N integers. The i-th integer is Vi.
Output
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the number of record breaking days.
Limits
Time limit: 20 seconds per test set.
Memory limit: 1GB.
1 ≤ T ≤ 100.
0 ≤ Vi ≤ 2 × 105.
Test set 1
1 ≤ N ≤ 1000.
Test set 2
1 ≤ N ≤ 2 × 105 for at most 10 test cases.
For the remaining cases, 1 ≤ N ≤ 1000.
Sample
Input
Output
4
8
1 2 0 7 2 0 2 0
6
4 8 15 16 23 42
9
3 1 4 1 5 9 2 6 5
6
9 9 9 9 9 9
Case #1: 2
Case #2: 1
Case #3: 3
Case #4: 0
In Sample Case #1, the bold and underlined numbers in the following represent the record breaking days: 1 2 0 7 2 0 2 0.
In Sample Case #2, only the last day is a record breaking day.
In Sample Case #3, the first, the third, and the sixth days are record breaking days.
In Sample Case #4, there is no record breaking day.
-------------------------------------------------------------------------
This python code which i submitted in 'Kickstart Round D problem 1: Record Broker'. I executed this code on my local machine and there wasn't any run time error on top of that I couldn't brute force any test case that could break the code or give wrong answer. But while doing submission in kickstart it gives me runtime error. What could be the issue for getting run time error on kickstart? Please help!
cases = int(input().strip())
for q in range(1, cases + 1):
l = int(input().strip())
ls = list(map(int, input().split(" ")))
l = len(ls)
local_max = 0
count = 0
for i in range(l):
if i == 0:
if ls[1] < ls[0]:
local_max = ls[0]
count += 1
continue
if i == l - 1:
if ls[i] > ls[i - 1] and ls[i] > local_max:
count += 1
break
if ls[i] > ls[i - 1] and ls[i] > ls[i + 1] and ls[i] > local_max:
count += 1
local_max = ls[i]
continue
print("Case #{}: {}".format(q, count))
this line:
if ls[1] < ls[0]:
What if ls only has 1 element?
After fixing that your code still returns the wrong answer if there's only 1 element.
Also, consider this test case:
"65 87 87 34 64 59 93 20 95 85 24 99 62 100 60 19 100"
64 is not valid, but you count it.
The sequence look like this 112123123412345...
If the input is 55,it should return 1,not 10. And if the input is 56,it should return 0,not 1. You got the idea.
So we have a sequence composed of the composition of
1 1 digit 1 digit total
12 2 digits 3 digits total
123 3 digits 3*(3+1)/2 = 6 digits total
1234 4 digits 4*(4+1)/2 = 10 digits total
...
123..89 9 digits 9*(9+1)/2 = 45 digits total
123..8910 11 digits 10*(10+1)/2 + 1 = 56
123..891011 13 digits 11*(10+1)/2 + 3 = 69
123..89101112 15 digits 12*(12+1)/2 + 3*(3+1)/2 = 84 digits
This is OEIS Sequence A165145 and also see the related sequence OEIS A058183.
A formula for the total number of digits is
f(n) = n*(n+1)/2 + {(n-9)*(n-8)/2 : if n>=10} + {(n-99)*(n-98)/2 : if n>=100) + ...
Some key points f(9) = 45, f(99) = 99*100/2 + 90*91/2 = 9045, f(999) = 1,395,495.
An outline algorithm for finding the k-th digit would be
Find which part of the sequence you are in n<=9, 10 <= n <= 99, 100 <= n <= 999 by comparing k with the boundary values 45, 9045, 1395495.
Recover the value of n
Find the actual digit which will be k-f(n) along sequence for the n-th number.
If we take 10 <= n <= 99 the formula for the number of digits is
n*(n+1)/2 + (n-9)*(n-8)/2
= 1/2( n^2 + n + n^2 - 17 n + 72)
= n^2 - 8 n + 36
So given 45 < k <= 9045 we solve k = n^2 - 8 n + 36, using the quadratic formula
n = ceil( ( 8 + sqrt(64 - 4 (36 - k)))/2)
We need a different quadratic formula for k outside the range.
For example take k = 100, using the formula gives n=13. There are f(12)=84 digits for all the numbers upto 12, so the first digit of the 13th string is at position 85. So we are looking for the 16th digit. We can use the formula
digit(l) := l <= 9 ? l : (l%2==0 ? floor((l+10)/20) : ((l-11)/2)%10 )
to find the actual digit, which is 1.
The result of the code includes separetly "min" and "max" values. Can't get how I can print only result of cicle?
For examle, whan I set min = 1, max = 3 the result is:
1
3
1
2
3
And I need 1 2 3 without first 1 3.
min = int(input())
max = int(input())
while min <= max:
print(min)
min = min + 1
That depends entirely on the ide or python environment you are using. May I know which environment you are using?
Because when I ran here https://ideone.com/lUmjKL everything seems fine.
min = int(input())
max = int(input())
while min <= max:
print(min)
min = min + 1
Let me know.
Once, one boy's teacher asked him to calculate the sum of numbers 1 through n.
the boy quickly answered, and his teacher made him another challenge. He asked him to calculate the sum of the digits of numbers 1 through n.
Input
Two space-separated integers 0 <= a <= b <= 109.
Output
The sum of the digits of numbers a through b.
Example
Input:
1 10
Output: 46
can someone explain what is meant by sum of the digits of numbers a to b?
from above, sum of {1 2 3 4 5 6 7 8 9 10 } is 55 , it is a well known Gaussian formula
but the output is 46!
if i count from 2 to 9, excluding the border numbers 1 and 10, the answer is 44 , still not 46
So what is meant by sum of digits of numbers?
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + (1 + 0)
Don't treat the 10 as the number 10, rather the digits 1 and 0
How do I write the formula that would mean this: If X<$999.00 then multiply by 0% If X >$1000.00 but < $1499.00 then multiply by 10% If X > $1499.00 but < $1999.00 then multiply by 15% If X > $1999.00 then multiply by $20%?
I think this meets the specifications of your question, but some things are ambiguous (your conditions exclude everything between $999.00 and $1000.00 inclusive, for example) so I guessed at what your real intent was.
This formula will return:
0 if X < $1000
10% of X if X >= $1000 and X < $1500
15% of X if X >= $1500 and X < $2000
20% of X in all other cases
=IF(X < 1000, 0, IF(X < 1500, X * 0.1, IF(X < 2000, X * 0.15, X * 0.2)))