The condition of the problem is such that the sum of the two first numbers can be equal to slightly less than 2 * (10 ^ 9), i.e. It takes about 2 GB, and this is more than the limit of 16 MB !!! How to get around? Help solve.

Input file name in.txt Output file name standard output Memory limit 16 megabytes

The beginning of the in.txt file contains two integers. If you add them, then you get the position in the file, starting with which is the third number that you need to display.

Input Format The file in.txt , formed according to the principle described above. It is guaranteed that each of the numbers is integer and does not exceed 10 ^ 9. It is also guaranteed that the sum of the first two numbers will not be a negative number.

Examples

Input data:

6 5 blah 25491

Output:

491

Problem solved, thanks Harry, here is the correct code:

 #include <iostream> #include <fstream> using namespace std; int main(){ ifstream infile; infile.open("in.txt", ios::in); int a, b, j = 0, pos1; //вывел два первых числа и сложил их infile >> a >> b; j = a+b; // потом, как Вы и сказали сдвинул infile.seekg(j, ios:: beg); infile >> pos1; cout << pos1; infile.close(); return 0; } 
  • but where does the assumption that the sum of the first two numbers can be is equal to slightly less than 2 * (10 ^ 9) ? - Grundy
  • "It is guaranteed that each of the numbers is integer and does not exceed 10 ^ 9" - Trulimon
  • Well, no more than 1_000_000_000 is correct - that is, 9 characters. - Grundy

1 answer 1

What's the problem? Read the first number, for absolutely no guarantee of overflows - in long long int , the second, find the offset, use seekg to get to the right place in the file, read and print the third number ...

No problem.

 ifstream in("input.txt",ios::binary); int pos1, pos2, num; in >> pos1 >> pos2; in.seekg(pos1+pos2); in >> num; cout << num; 
  • 1 character takes 1 byte, 1 000 000 000 characters will take 1 GB. In the condition of the memory limit of 16 MB is 100 times less. - Trulimon
  • one
    Where did you get so many characters? Do you want to read the entire file into memory? :) Once again and carefully re-read the condition of the problem. - Harry
  • ))))))))) Yes, indeed, you are right, it's only 9 characters, and not a billion !!! - Trulimon
  • Everything turned out great: - Trulimon
  • Well, then mark the answer as accepted ... - Harry