Problem A 2016, 12th Computing Program Design Competition for 2016 College Students in Hunan, Hunan 2016, China
Problem A: 2016 Time Limit: 5 Sec Memory Limit: 128 MB
Description
Returns positive integers n and m, and counts the number of positive integers (a, B) that meet the following conditions: 1. 1 ≤ a ≤ n, 1 ≤ B ≤ m; 2. a × B is a multiple of 2016. Input
The input contains no more than 30 groups of data. Each group of data contains two integers, n, and m (1 ≤ n, m ≤ 109). For each group of data, an integer is Output to indicate the number of data that meets the conditions. Sample Input
32 632016 20161000000000 1000000000
Sample Output
1305767523146895502644
The question is full of Chinese characters, so there is nothing to say about the question, and it is also easy to understand, mainly to consider the practice. First, the data is very big. It is certainly not enough to run the for loop one by one. But in the example, we will tell you the biggest case, we can understand that the long definition is enough, and then we can consider how to deal with it.
Because the question requires that the two numbers are multiplied by a factor of 2016, you can separate them, for example, 1 ~ 2016 and 2017 ~ 4032 the two groups of data have different numbers, but the results are the same if the two groups are multiplied by other numbers to determine how many numbers are multiples of 2016, therefore, you can split the two sets of numbers before performing operations. In this way, you only need to run the for loop twice and only 2016 is enough, separate the remaining numbers and store them with arrays. record the total number of groups 1 ~ 2016. Multiply the result by a few.
The following AC code
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> using namespace std; long a [2030], B [2030]; int main () {long n, m; int I, j; long k, t; long cou; while (scanf ("% lld", & n, & m )! = EOF) {cou = 0; // separate n k = n/2016; t = n % 2016; for (I = 1; I <= t; I ++) {a [I] = k + 1 ;}for (I = t + 1; I <= 2016; I ++) {a [I] = k ;} // separate m k = m/2016; t = m % 2016; for (I = 1; I <= t; I ++) {B [I] = k + 1;} for (I = t + 1; I <= 2016; I ++) {B [I] = k ;} // calculate for (I = 1; I <= 2016; I ++) {for (j = 1; j <= 2016; j ++) {if (I * j) % 2016 = 0) cou = cou + a [I] * B [j] ;}} cout <cou <endl ;} return 0 ;}