How can you calculate the shortest chain of addition for an arbitrary n <= 600 in one second?

How can you calculate the shortest chain of addition (sac) for an arbitrary n <= 600 in one second?

Notes

This month's codility programming competition .

Additive chains are very important because they are the most economical way to calculate x ^ n (sequential multiplications).

Knuth The Art of Computer Programming, Volume 2, Seven-Dimensional Algorithms has a good understanding of additive chains and some interesting properties, but I didn’t know anything that allowed me to meet strict performance requirements.

( )

-, ( ) ( 1- > 2 → (3 → ..., 4 → ...)) , node n, n n. > 400 , .

, . 600, . n n. , ...

NP-hard, . , , , . .

Update

, , , , / / ?

+5
2

. , - . :

  • , .
  • n < 12509
  • , .
  • , .
  • , , .

.

+4

, x ^ n ( ).

. x ^ n. Graham et. , :

, , "" .

x ^ n (mod m), , , .

, . , .

(aka star-chain) , (, ). n < 12509. . J. Bernstein:

"- 2 ^ k- ", "2 ^ k- ". . ; n n. .

BTW. - C/++ ? : x ^ n x ^ n (mod m).

+1

All Articles