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.
分析; 对于任何一个数,在 10个数之内,一定可以找到两个数相加是5的倍数,然后就判断最后一个数,就可以了!
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<set>
using namespace std;
int main()
{
int n,m;
while(cin>>n>>m)
{
long long sum=0;
for(int i=1;i<=n;i++)
{
if(i<=5)
{
int k=m/10;
int mod=m%10;
sum+=k*2;
if(mod>=(10-i))
sum+=2;
else if(mod>=(5-i))
sum+=1;
if(i==5)
sum--;
}
else{
int k=m/10;
int mod=m%10;
sum+=k*2;
int mod1=i%10;
if(mod1==5&&mod==0)
continue;
if(mod1>=5)
{
if(mod>=(15-mod1))
sum+=2;
else if(mod>=(10-mod1))
sum++;
}
else
{
if(mod>=(10-mod1))
sum+=2;
else if(mod>=(5-mod1))
sum++;
}
}
}
cout<<sum<<endl;
}
return 0;
}
本文介绍了一个关于整数配对的问题:给定两个整数n和m,如何计算第一列从1到n和第二列从1到m的整数中,能够找到多少对整数(x, y),使得它们的和能被5整除。文章提供了详细的解决思路及C++代码实现。
406

被折叠的 条评论
为什么被折叠?



