原题描述:
Dr. Tuple is working on the new data-mining application for Advanced Commercial Merchandise Inc.One of the subroutines for this application works with two arrays P and Q containing N records of dataeach (records are numbered from 0 to N − 1). Array P contains hash-like structure with keys. ArrayP is used to locate record for processing and the data for the corresponding record is later retrievedfrom the array Q.All records in array P have a size of SP bytes and records in array Q have size of SQ bytes. Dr.Tuple needs to implement this subroutine with the highest possible performance because it is a hot-spotof the whole data-mining application. However, SP and SQ are only known at run-time of applicationwhich complicates or makes impossible to make certain well-known compile-time optimizations.The straightforward way to find byte-offset of i-th record in array P is to use the following formula:Pofs(i) = SP · i, (1)and the following formula for array Q:Qofs(i) = SQ · i. (2)However, multiplication computes much slower than addition or subtraction in modern processors.Dr. Tuple avoids usage of multiplication while scanning array P by keeping computed byte-offsetPofs(i) of i-th record instead of its index i in all other data-structures of data-mining application. Heuses the following simple formulae when he needs to compute byte-offset of the record that precedes orfollows i-th record in array P:Pofs(i + 1) = Pofs(i) + SPPofs(i − 1) = Pofs(i) − SPWhenever a record from array P is located by either scanning of the array or by taking Pofs(i) fromother data structures, Dr. Tuple needs to retrieve information from the corresponding record in arrayQ. To access record in array Q its byte-offset Qofs(i) needs to be computed. One can immediatelyderive formula to compute Qofs(i) with known Pofs(i) from formulae (1) and (2):Qofs(i) = Pofs(i)/SP · SQ (3)Unfortunately, this formula not only contains multiplication, but also contains division. Even thoughonly integer division is required here, it is still an order of magnitude slower than multiplication onmodern processors. If coded this way, its computation is going to consume the most of CPU time indata-mining application for ACM Inc.After some research Dr. Tuple has discovered that he can replace formula (3) with the followingfast formula:Qofs’(i) = (Pofs(i) + Pofs(i) << A) >> B (4)where A and B are non-negative integer numbers, “<< A” is left shift by A bits (equivalent to integermultiplication by 2A), “ >> B” is right shift by B bits (equivalent to integer division by 2B).This formula is an order of magnitude faster than (3) to compute, but it generally cannot alwaysproduce the same result as (3) regardless of the choice for values of A and B. It still can be used if oneis willing to sacrifice some extra memory.Conventional layout of array Q in memory (using formula (2)) requires N · SQ bytes to store theentire array. Dr. Tuple has found that one can always choose such K that if he allocates K bytes ofmemory for the array Q (where K ≤ N · SQ) and carefully selects values for A and B, the fast formula(4) will give non-overlapping storage locations for each of the N records of array Q.Your task is to write a program that finds minimal possible amount of memory K that needs tobe allocated for array Q when formula (4) is used. Corresponding values for A and B are also to befound. If multiple pairs of values for A and B give the same minimal amount of memory K, then thepair where A is minimal have to be found, and if there is still several possibilities, the one where Bis minimal. You shall assume that integer registers that will be used to compute formula (4) are wideenough so that overflow will never occur.InputInput consists of several datasets. Each dataset consists of three integer numbers N, SP , and SQseparated by spaces (1 ≤ N ≤ 220, 1 ≤ SP ≤ 210, 1 ≤ SQ ≤ 210).OutputFor each dataset, write to the output file a single line with three integer numbers K, A, and B separatedby spaces.
Sample Input
20 3 5
1024 7 1
Sample Output
119 0 0
1119 2 5
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
long long int n, Sp, Sq;
while (scanf("%lld%lld%lld", &n, &Sp, &Sq) != EOF)
{
long long int A, B, K;
K = n*Sq<<10;
long long int temp;
for (long long int i = 0; i < 32; i++)
for (long long int j = 0; j < 32; j++)
{
temp = (((n - 1)*Sp + ((n - 1)*Sp << i)) >> j) + Sq; //当A,B分别为i,j时存放Q数组用的字节数
if (temp >= Sq*n && temp < K)
{
K = temp;
A = i;
B = j;
}
}
cout << K << " " << A << " " << B << endl;
}
}