-7

No data type can store such large number. Using array int a[pow(10,pow(10,18))] again won't do the job because pow() returns double and double can't store 10^(10^18).

Anyone having any idea?

I'm trying to solve the following problem:

Consider an integer with N digits (in decimal notation, without leading zeroes) D1,D2,D3,…,DN. Here, D1 is the most significant digit and DN the least significant. The weight of this integer is defined as:

∑ i=2 -> N (Di−Di−1).

You are given integers N and W. Find the number of positive integers with N digits (without leading zeroes) and weight equal to W. Compute this number modulo 109+7.

Input:

The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first and only line of each test case contains two space-separated integers N and W denoting the number of digits and the required weight.

Output:

For each test case, print a single line containing one integer — the number of N-digit positive integers with weight W, modulo 109+7.

Constraints:

  • 1≤T≤105
  • 2≤N≤1018
  • |W|≤300
dbush
  • 205,898
  • 23
  • 218
  • 273
  • 5
    What are you trying to do with numbers of that size? – dbush Apr 09 '18 at 18:36
  • 6
    if each digit is one byte then that would take 1 million TB. So you cant – pm100 Apr 09 '18 at 18:36
  • "Store" it in whatever symbolic form you're using to describe it now and operate on that representation? – Alex Celeste Apr 09 '18 at 18:37
  • maybe `long double`... or some BIGINT, but whatever you will never have enough memory and array size can't be floating number or BIGINT. You max will always be `SIZE_MAX` – Stargateur Apr 09 '18 at 18:37
  • char* for storing? With some conversion of course. – camel-man Apr 09 '18 at 18:37
  • 2
    How big do you think 10^18 is?? What is the ***real*** problem you are trying to solve, because this problem is not solvable within this universe. – abelenky Apr 09 '18 at 18:38
  • 6
    More to the point, are you really asking about 10^(10^18) ?? – Steve Summit Apr 09 '18 at 18:39
  • I guess this isn't as bad as [iterating all GUIDs](https://stackoverflow.com/questions/10029651/fastest-way-in-c-sharp-to-iterate-through-all-guids-possible). (10k link) – Mysticial Apr 09 '18 at 18:39
  • https://www.codechef.com/APRIL18B/problems/WGHTNUM in this question N is the number of digits and it can be 10^18(see constraints) – Utkarsh Pandey Apr 09 '18 at 18:39
  • 4
    @UtkarshPandey That's asking for 10^18, meaning 1,000,000,000,000,000,000. You're asking for 10^10^18 which is so large I can't write it down here. – Schwern Apr 09 '18 at 18:41
  • @schwern that's just 18 digits. In the problem, N is the NUMBER of DIGITS and it can be 10^18 – Utkarsh Pandey Apr 09 '18 at 18:43
  • Can you tell us why you want to represent numbers that big - this might give some clues about how to represent them? 10^18 digits is more than you could store in about 370 PB of RAM. – rwp Apr 09 '18 at 18:43
  • I wonder if its a mis-wording, it does say *"Consider an integer with N digits"* and later *"2 ≤ N ≤ 10^18"* which sounds like it matches the question to me – kmdreko Apr 09 '18 at 18:43
  • Are you trying to calculate the number of atoms in the universe? – camel-man Apr 09 '18 at 18:44
  • @Schwern No, N is the number of digits. But the algorithm they are meaning is obviously not asking to store these. Could be a mistake in the question too... – Eugene Sh. Apr 09 '18 at 18:45
  • 1
    You don't need to store a number of that size. Look closely at the definition for the weight. – dbush Apr 09 '18 at 18:46
  • 2
    @UtkarshPandey I suspect you're supposed to solve the problem without actually storing 10^10^18. – Schwern Apr 09 '18 at 18:48
  • 1
    Deja-vu, second post I've seen with 'modulo 10^9+7' in it. It's not something you forget easily... – Martin James Apr 09 '18 at 18:58
  • Oh look! Entering 'modulo 10^9+7' into a popular search engine gives 'About 48,600,000 results'. Maybe one of those could help? – Martin James Apr 09 '18 at 19:00
  • Possible duplicate of [what is the significance of modulo 10^9+7 used in codechef and spoj problems?](https://stackoverflow.com/questions/25689186/what-is-the-significance-of-modulo-1097-used-in-codechef-and-spoj-problems) – Bo Persson Apr 09 '18 at 20:41
  • You need to use your creativity to find a way to solve this problem without iterating over all the 10¹⁸-digit numbers. That's really the point of the challenge, so it's not appropriate to spoon-feed the answer here. – Toby Speight Apr 10 '18 at 15:24

3 Answers3

7

You don't need to store a number with 10^18 digits. Look at the definition of the weight:

∑ i=2 -> N (Di−Di−1)

Each element in the sum is the difference of two consecutive digits.

Let's take for example a 4 digit number whose digits are D1, D2, D3, D4. Then the sum is:

(D2 - D1) + (D3 - D2) + (D4 - D3)

Reording the operands:

D4 - D3 + D3 - D2 + D2 - D1

You'll see that all but the first and last digits cancel out! So the whole sum is D4 - D1. In fact, for any number of digits N, the sum is:

DN - D1

So only the first and the last digits are relevant. You should be able to figure out the rest from there.

dbush
  • 205,898
  • 23
  • 218
  • 273
0

There are libraries for handling these sorts of problems, like The GNU Multiple Precision Arithmetic Library:

What is GMP?

GMP is a free library for arbitrary precision arithmetic, operating on signed integers, rational numbers, and floating-point numbers. There is no practical limit to the precision except the ones implied by the available memory in the machine GMP runs on. GMP has a rich set of functions, and the functions have a regular interface.

The main target applications for GMP are cryptography applications and research, Internet security applications, algebra systems, computational algebra research, etc.

But 10^18 would take a huge (and effectively impossible) amount of memory (if my math is correct: 2.881 EiB).

gregmac
  • 24,276
  • 10
  • 87
  • 118
  • 1
    10^18 is way bigger than that. It is impossible on any single modern computer. It would require a massive distributed system. – abelenky Apr 09 '18 at 18:43
0

The referenced problem takes input from 2 ≤ N ≤ 10^18. That isn't 10^18 digits (or 10^10^18 which is absurdly enormous) but 18 digits or 2 to 1,000,000,000,000,000,000. This will fit inside a 64 bit integer, signed or unsigned.

Use int64_t from stdint.h.

10^18 is pushing the limits of 64 bit integers, probably why they chose it. Anything larger should use an arbitrary precision math library such as GMP.

...but there are limits. Simply storing such a number would take about 1 million gigabytes. So while the problem is about solving for numbers with 10^18 digits, I strongly suspect you're not supposed to solve it by actually storing those numbers. There's some mathematical technique you're supposed to apply.

Schwern
  • 153,029
  • 25
  • 195
  • 336
  • No! N is the number of digits. According to the original problem at least. – Eugene Sh. Apr 09 '18 at 18:48
  • 3
    @EugeneSh. I'm pretty sure you're supposed to solve the problem without actually storing 10^10^18. – Schwern Apr 09 '18 at 18:49
  • 1
    That's for sure. But your answer is incorrect. – Eugene Sh. Apr 09 '18 at 18:49
  • @EugeneSh. Part of asking an expert is finding out you're asking the wrong question. :) – Schwern Apr 09 '18 at 18:52
  • 1
    Again, everything but the last two paragraphs of your answer is plainly wrong with the relation to the question. I don't know how it is getting upvoted. – Eugene Sh. Apr 09 '18 at 18:53
  • @EugeneSh. Yes, I could tell them you can't store 10^10^18 and that would be a technically correct answer to their stated question. But it doesn't solve their problem. Look into the [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) for more. – Schwern Apr 09 '18 at 18:55
  • 1
    No! This is not the problem with the answer. The problem that you give a wrong interpretation of a well defined question. – Eugene Sh. Apr 09 '18 at 18:55
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/168585/discussion-between-schwern-and-eugene-sh). – Schwern Apr 09 '18 at 18:56