Feeding the Herrings

Count ordered triples summing to N with each part at least L and no digit 3 in any part, modulo 12345647.

Hard8Dynamic programmingCombinatoricsMathNo attempts yetTime limit5sMemory limit256 MB

Problem

Zookeeper Willy is handing out herrings to the seals today. The seals live in three separate pools. The zoo requires its keepers to record what the animals eat, so a touchscreen is installed next to the pools and Willy has to type in the number of herrings he puts into each of the three pools. The screen is broken: it cannot accept the digit 3.

Willy called the chief keeper of marine mammals and asked for help.

"That is fine," said the chief. "Just split the herrings so that the number going into each pool has no digit 3 in it."

"But each pool needs at least LL herrings," Willy answered. "I might not find a split that works."

"You will find one," said the chief. "With that many herrings in the bucket there are countless possible splits."

"Exactly how many?" Willy wondered to himself.

You are given the total number NN of herrings and the lower limit LL on the number of herrings in each pool. Count the splits of the NN herrings into the three pools in which every pool gets at least LL herrings, the decimal representation of each of the three numbers contains no digit 3, and the three numbers add up to exactly NN. Individual herrings are not distinguished, since they are all about the same size and nutrition value. The pools are distinguished, because different groups of seals live in them. No herring can be cut into pieces.

Input

The input holds several test cases. Each case is one line with two integers NN and LL (1N10100001 \le N \le 10^{10000}, 1LN/31 \le L \le N/3) separated by a space, the number of herrings in the bucket and the lower limit on the number of herrings in each pool. A line holding two zeros ends the input.

Output

For each test case print on its own line the number of ways to split the herrings into the three pools, modulo 12345647.