This is spoj's Playing with gcd id najpwg http://www.spoj.com/problems/NAJPWG/ [closed] - greatest-common-divisor

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm getting tle in this code any suggestions.I'm calculating the sum n/i where
n is the input and i goes from 2 to n.
for example for 5 pairs will be (2,2),(3,3),(4,4),(5,5),(2,4)
#include<stdio.h>
‏
int main()
{
int i,j,t,n,m;
long long k;
scanf("%d",&t);
for(i=0;i<t;i++)
{
scanf("%d",&n);
m=n/2;
k=0;
for(j=2;j<=m;j++)
{
k+=(n/j);
}
k+=(n-m);
printf("Case %d: %lld\n",i+1,k);
}
return 0;
}`

There are a lot of problems with your code.
Broadly classifying -
Optimization can be talked about when the issue no.2 gets resolved.
Code gives wrong answer. Your algorithm is,summarily, incorrect. Dump this approach and think in terms of "Euler Totient" and elementary dynamic programming.
For example,
Let's say, the number is N.
Now, N would contain all the pairs that N-1 had. In addition to that it would also contain some newer ones( for which you can take help of Euler Totient function).
Sample Test cases for your practice -
input-
25
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1021
99998
99999
100000
output-
Case 1: 0
Case 2: 0
Case 3: 1
Case 4: 2
Case 5: 4
Case 6: 5
Case 7: 9
Case 8: 10
Case 9: 14
Case 10: 17
Case 11: 23
Case 12: 24
Case 13: 32
Case 14: 33
Case 15: 41
Case 16: 48
Case 17: 56
Case 18: 57
Case 19: 69
Case 20: 70
Case 21: 82
Case 22: 204311
Case 23: 1960304047
Case 24: 1960339246
Case 25: 1960399246
*

Related

Level and experience algorithm

Can we get either the level up XP or total XP from a closed formula? If so, what formula?
This is from gambling site i have found and the "Daily" is money you can get every 24hours
Is there any algorithm I can follow to get something like that? Thank you for your ideas
Level n total xp = 30 * (n-1)^4
2: 30 * 1^4 = 30
3: 30 * 2^4 = 480
...
40: 30 * 39^4 = 69,403,230
Found via prime factorization.
$ factor 69403230
69403230: 2 3 3 3 3 3 5 13 13 13 13
$ factor 62554080
62554080: 2 2 2 2 2 3 5 19 19 19 19

Ada: Integer Overflow

so I am hashing and have defined these types/functions:
subtype string2 is String(1..2);
function cString2 is new Ada.Unchecked_Conversion(string2, long_integer);
function cChar is new Ada.Unchecked_Conversion(character, long_integer);
and MUST use this hash function:
HA = (((cString2(s1) + cString2(s2)) * 256) + cChar(char)) mod 128
(the function is bad on purpose, but I must implement it) The problem occurs when adding and/or trying to multiply 256 by the sum of the two long integers, for it overflows. I need to somehow treat the strings as POSITIVE integer values and also not have my function overflow. THANKS!!!
The type Long_Integer is a signed integer type, and guaranteed to cover the range –2**31+1 .. +2**31–1 (if it exists):
LRM 3.5.4(22):
If Long_Integer is predefined for an implementation, then its range shall include the range –2**31+1 .. +2**31–1.
With your declarations you are likely to include at least 2 bytes of random junk in your converted values, but as the sizes don't match, the result is implementation defined and possibly invalid or abnormal.
I suggest that you read up on the 'Pos attribute and Ada.Unchecked_Conversion in the LRM.
You can compare the quality of various Hash functions using the approach shown here, which tallies collisions in a hash table of dictionary words. The resulting Counts are stored in an instance of Ada.Containers.Ordered_Maps.
As a concrete example, the library Hash function
function Hash is new Ada.Strings.Bounded.Hash(ASB);
produces a result having unique hashes for over half of the words and just seven collisions in the worst case:
Word count: 235886
Table size: 393241
Load factor: 59.99%
0: 215725 (0.00%)
1: 129710 (54.99%)
2: 38727 (32.84%)
3: 7768 (9.88%)
4: 1153 (1.96%)
5: 143 (0.30%)
6: 14 (0.04%)
7: 1 (0.00%)
In contrast, this Hash function
function Hash(Key : ASB.Bounded_String) return Ada.Containers.Hash_Type is
S : String := ASB.To_String(Key);
H : Ada.Containers.Hash_Type := 0;
begin
for C of S loop
H := H * 3 + Character'Pos(C);
end loop;
return H;
end;
produces a result having unique hashes for fewer than half of the words and twenty collisions each for two different hash values in the worst case:
Word count: 235886
Table size: 393241
Load factor: 59.99%
0: 236804 (0.00%)
1: 107721 (45.67%)
2: 32247 (27.34%)
3: 9763 (12.42%)
4: 3427 (5.81%)
5: 1431 (3.03%)
6: 813 (2.07%)
7: 441 (1.31%)
8: 250 (0.85%)
9: 150 (0.57%)
10: 88 (0.37%)
11: 41 (0.19%)
12: 27 (0.14%)
13: 14 (0.08%)
14: 11 (0.07%)
15: 7 (0.04%)
16: 2 (0.01%)
17: 1 (0.01%)
19: 1 (0.01%)
20: 2 (0.02%)

matrix exponentiation in linear recursion algorithm

I am trying to do http://www.spoj.com/problems/FIBTWIST/ problem by linear recursion. However, since the constraints are large I have to use matrix exponentiation.
I have read http://zobayer.blogspot.in/2010/11/matrix-exponentiation.html
so according to it equations formed are
ft(n)=ft(n-1)+ft(n-2)+g(n) ft(0)=0, ft(0)=1
g(n) =g(n-1)+1 g(1)=0
But now I am confused how to form matrices A and B of the form A*M=B. It is given as Type 7 in mentioned blogspot link but I am having difficulty in understanding it.
Define a third sequence, fut, Fibonacci-untwist, as
fut(n)=ft(n)+(n+2).
Then
fut(n)=ft(n)+n+1=ft(n-1)+ft(n-2)+(n-1)+(n+2)=fut(n-2)+fut(n-1)
So fut is just another solution of the Fibonacci recursion, and thus
fut(n)=f(n-1)*fut(0)+f(n)*fut(1)=2*f(n-1)+4*f(n)=2*f(n)+2*f(n+1)=2*f(n+2)
and finally
ft(n)=2*f(n+2)-(n+2)
Test:
f(n): 0 1 1 2 3 5 8 13 21 34
2*f(n+2): 2 4 6 10 16 26 42 68
n+2: 2 3 4 5 6 7 8 9
ft(n): 0 1 2 5 10 19 34 59
and really, the last row is the difference of the second and third row.

fastest way to perform divison without using / or * operator any other solution [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I'm following the Skiena's algorithm book. In chapter 1, it has gives the problem to divide a real number without using / or * operator.
I implemented that in python like this:
def main(dividend, divisor ):
print "dividend : ",dividend
print "divisor : ", divisor
if dividend<divisor:
print "dividend has to be greater than divisor"
return;
quotient=0
sum=0
while sum<dividend:
sum = sum+divisor
print "sum : ",sum
quotient = quotient+1
if sum>dividend:
remainder = dividend-(sum-divisor)
quotient=quotient-1
print "remainder : ", remainder
print "quotient :", quotient
if __name__ == "__main__":
# pass any two real number as dividend and divisor
main(29, 3)
The problem also stated that you must, "find the fastest way to do it". Is there any other way to solve the problem more quickly?
I'm not going to write code for you, but a fast way to do this uses powers of 2 and only the addition and subtraction operators. Let's consider as an example 973 / 47. First build a table of powers of 2 times the divisor; you can do that with simple addition:
1: 47
2: 94
4: 188
8: 376
16: 752
32: 1504
Each power of 2 is just the previous power of 2 added to itself. Stop when the power of 2 exceeds the dividend. Now work backward, subtracting a power of 2 if it is less than the remaining dividend:
16: 973 - 752 = 221
4: 221 - 188 = 33
So the quotient is 16 + 4 = 20 and the remainder is 33.
This algorithm is similar to the peasant algorithm for multiplication, which I discuss at my blog.

Combinatorics, probability, dice

A friend of mine asked: if I have two dice and I throw both of them, what is the most frequent sum (of the two dice' numbers)?
I wrote a small script:
from random import randrange
d = dict((i, 0) for i in range(2, 13))
for i in xrange(100000):
d[randrange(1, 7) + randrange(1, 7)] += 1
print d
Which prints:
2: 2770,
3: 5547,
4: 8379,
5: 10972,
6: 13911,
7: 16610,
8: 14010,
9: 11138,
10: 8372,
11: 5545,
12: 2746
The question I have, why is 11 more frequent than 12? In both cases there is only one way (or two, if you count reverse too) how to get such sum (5 + 6, 6 + 6), so I expected the same probability..?
In both cases there is only one way
(or two, if you count reverse too)
There are two ways. If the dice are named A and B:
12 = one way: A=6, B=6
11 = two ways: A=5, B=6 and A=6, B=5.
The question I have, why is 11 more frequent than 12?
First of all, this question assumes that your arbitrary try gives an authoritative result. It doesn’t; the result is pure random and only reliable up to a degree. But in this particular case, the numbers actually reflect the real proportions nicely.
That said, there are two ways to get 11: 5 (first die) + 6 (second die) and 6 (first die) + 5 (second die) but only one way to get 12: 6 (first die) + 6 (second die).
The most frequently met sum is 7, as suggested by your empirical test.
Now, to answer your questions specifically:
11 is more frequent than 12 because you get 12 by rolling 6,6, but you can get 11 by 5,6 or 6,5, which is double the probability.
Based on classic probability theory, the probability of an event occurring is equal to (number-of-beneficial-simple-events-that-trigger-it)/(number-of-all-possible-events). So using this simple formula yields that in order to get a 7, you need to roll one of the following combinations: (1,6), (2,5), (3,4), (4,3), (5,2), (6,1), and you have 6x6=36 events all together. The chance of getting a 7 is P = 6/36 = 1/6, which is as high as it gets.
Check out Probability for more info.
For 11 there is 5 + 6 and 6 + 5 for 12 there is only 6 + 6.
You can observe the same thing with 2 and 3.

Resources