|
|
вернуться в форумMath hint and some more ... Послано Anton 18 окт 2011 13:36 First of all. If you get WA#1, check how you scan input stream, cause they've put some trash in file. I used the following idea. The whole number with base k divisible by ( k - 1 ), if sum( div( digit[ i ] ) ) is divisible by ( k - 1 ). Where digit[ i ] is k-based digit in input number ( char from input stream ); div( digit[ i ] ) is a remainder of division digit[ i ] * k by ( k - 1 ). Let see digit on i-th position, for example, it's A. So in decimal implementation it's equal to A * k ^ i. A * k ^ i = A * k^i - A * k^(i-1) + A * k^(i-1) = A * k^(i-1) * ( k - 1 ) + A * k^(i-1) A * k^(i-1) * ( k - 1 ) is divisible by ( k - 1 ), so we have to check only A * k^(i-1). If we repeat the same logic to this expression (i - 2) times, we see, that we have to check division A * k / ( k - 1 ). I tried my best to explain the main idea, sorry if it can't be understood. Re: Math hint and some more ... You don't need your "div()". Re: Math hint and some more ... Isn't it okay to just find the digital sum of the given number and add 1? Re: Math hint and some more ... Thank you. And we even can go a step further. Now see, A * k = A * k - A + A = A(k - 1) + A, So what we need to examine is just whether sum( digit[i]) is divisible by ( k - 1). |
|
|