D - Alyona and Numbers
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit

Status

Practice

CodeForces 682A
Description
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.

Sample Input
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
Hint
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.

AC代码:

#include <stdio.h>
int main()  
{  
    int n,m,i;  
    long long  sum=0;  
    scanf("%d%d",&n,&m);  
    for(i=1;i<=n;i++)        
        sum+=((m+i)/5-i/5);      
    printf("%lld",sum);  
    return 0;  
} 

题意:给出两个整数n,m;两组整数从1~n和1~m,交错相加,如果两个数相加是5的倍数,则给计数器加1,最后总的输出计数器的大小。
错误点在于:很多人使用外循环n和内循环m,这样的话算法太复杂,程序超时,无法AC,故根据他们余数的特点,找出减少运算量的方法
即:(m+i)/5-i/5,还有注意sum的范围,超出int范围,使用long long

zsy:
AC代码:

#include<stdio.h>
int main()  
{  
    int n,m,i;  
    long long count=0;  
    scanf("%d %d",&n,&m);  
    for(i=1;i<=n;i++)  
    {  
        count+=((m+i)/5-i/5);  
    }  
    printf("%lld\n",count);  
    return 0;  
}