A. Alyona and Numbers
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
After finishing eating her bun, Alyona came up with two integers n and m. She decided to write down two columns of integers — the first column containing integers from 1 to n and the second containing integers from 1 to m. Now the girl wants to count how many pairs of integers she can choose, one from the first column and the other from the second column, such that their sum is divisible by 5.
Formally, Alyona wants to count the number of pairs of integers (x, y) such that 1 ≤ x ≤ n, 1 ≤ y ≤ m and equals 0.
As usual, Alyona has some troubles and asks you to help.
Input
The only line of the input contains two integers n and m (1 ≤ n, m ≤ 1 000 000).
Output
Print the only integer — the number of pairs of integers (x, y) such that 1 ≤ x ≤ n, 1 ≤ y ≤ m and (x + y) is divisible by 5.
Examples
input
6 12
output
14
input
11 14
output
31
input
1 5
output
1
input
3 8
output
5
input
5 7
output
7
input
21 21
output
88
Note
Following pairs are suitable in the first sample case:
for x = 1 fits y equal to 4 or 9;
for x = 2 fits y equal to 3 or 8;
for x = 3 fits y equal to 2, 7 or 12;
for x = 4 fits y equal to 1, 6 or 11;
for x = 5 fits y equal to 5 or 10;
for x = 6 fits y equal to 4 or 9.
Only the pair (1, 4) is suitable in the third sample case.
题目链接:http://codeforces.com/contest/682/problem/A
题意: 给你n和m个数,让你从1 - n 和 1-m 中分别取一个数,组成一个数对,求有多少个数对和为5的倍数。
因为今天一天连续好几个A题都是一路暴力过的,所以这道题想也没想,写了个O(n^2)的算法,TLE了,一开始看错n m取值范围,以为合理优化暴力还是能过的,仔细一看发现必须换方法了。
于是想办法用一层循环做,
想了半天想出一个最蠢的打表……
#include<iostream> using namespace std; void swap( int &a , int &b) { int temp; temp=a; a=b; b=temp; return ; } int main() { int n,m; cin >> n >> m ; long long int ans = 0 ; if( n > m) swap(n,m); int unit = m % 10 ; for ( int i = 1 ; i <=n ; i++) { if( i % 10 == 1 ) ans+=( ( unit >= 4 ) ? 1 : 0 ) + ( ( unit >=9 ) ? 1 : 0 ) + 2* (m / 10 ); if( i % 10 == 2 ) ans+=( ( unit >= 3 ) ? 1 : 0 ) + ( ( unit >=8 ) ? 1 : 0 ) + 2*(m / 10) ; if( i % 10 == 3 ) ans+=( ( unit >= 2 ) ? 1 : 0 ) + ( ( unit >=7 ) ? 1 : 0 ) + 2*(m / 10) ; if( i % 10 == 4 ) ans+=( ( unit >= 1 ) ? 1 : 0 ) + ( ( unit >=6 ) ? 1 : 0 ) + 2*(m / 10 ); if( i % 10 == 5 ) ans+= ( ( unit >=5 ) ? 1 : 0 ) + 2*( m / 10 ) ; if( i % 10 == 6 ) ans+=( ( unit >= 4 ) ? 1 : 0 ) + ( ( unit >=9 ) ? 1 : 0 ) + 2* ( m / 10 ); if( i % 10 == 7 ) ans+=( ( unit >= 3 ) ? 1 : 0 ) + ( ( unit >=8 ) ? 1 : 0 ) + 2*(m / 10 ); if( i % 10 == 8 ) ans+=( ( unit >= 2 ) ? 1 : 0 ) + ( ( unit >=7 ) ? 1 : 0 ) + 2*(m / 10 ); if( i % 10 == 9 ) ans+=( ( unit >= 1 ) ? 1 : 0 ) + ( ( unit >= 6 ) ? 1 : 0) + 2*(m / 10 ) ; if( i % 10 == 0 ) ans+=( ( unit >= 5 ) ? 1 : 0 ) + 2 * (m / 10) ; } cout << ans << endl ; return 0 ; }
然后觉得这样写太蠢了,而且我还因为运算优先级问
题WA了好几次就更蠢了(比如m / 2 没加括号,对于m > 5 且 m < 9的情况会有问题),于是搜了一下别人的题解
发现这样一个写法,代码如下:
#include<iostream> using namespace std; int main() { int n , m ; long long int ans = 0 ; cin >> n >> m; for( int i = 1 ; i <= n ; i++) { ans+= ( i + m ) / 5 - i / 5 ; } cout << ans << endl ; return 0 ; }
原理: 对于第i个数,只要看他和m相加之后有多少个5的倍数就是答案,需要注意的是如果i本身大于5,需要减去那一部分,如i=15的时候,5的倍数 5 10 1 5 这三个数就不能考虑了……
自己好蠢。