Codeforces Round #358 (Div. 2) A. Alyona and Numbers 水题

本题探讨了如何通过统计余数来快速计算两个整数范围内所有可能配对中,其和能被5整除的配对数量。通过将问题简化为余数的组合问题,并利用O(n)的时间复杂度来解决。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A. Alyona and Numbers

题目连接:

http://www.codeforces.com/contest/682/problem/A

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

6 12

Sample Output

14

Hint

题意

给你n,m。然后告诉你1<=x<=n,1<=y<=m

然后问你(x+y)%5=0的方案有多少种

题解:

考虑余数。

两个余数之和为0,那么有0+0,1+4,2+3,3+2,4+1这么五种组合,我可以O(n)或者O(5)统计出每个数的余数为i的有多少个。

然后再O(5)的求解答案就好了。

代码

#include<bits/stdc++.h>
using namespace std;

long long num1[5];
long long num2[5];
int main()
{
    long long n,m;
    cin>>n>>m;
    for(int i=0;i<5;i++)
    {
        num1[i]=n/5;
        if(n%5>=i)num1[i]++;
        num2[i]=m/5;
        if(m%5>=i)num2[i]++;
    }
    num1[0]--,num2[0]--;
    long long ans = 0;
    ans = num1[0]*num2[0]+num1[1]*num2[4]+num1[2]*num2[3]+num1[3]*num2[2]+num1[4]*num2[1];
    cout<<ans<<endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值