2016湖南省赛-A-2016 (思路)

来源:互联网 发布:手机抢网速软件 编辑:程序博客网 时间:2024/06/10 09:50

Problem A: 2016

Time Limit: 5 Sec  Memory Limit: 128 MB

Description

 给出正整数 n 和 m,统计满足以下条件的正整数对 (a,b) 的数量:
1. 1≤a≤n,1≤b≤m;
2. a×b 是 2016 的倍数。
 

Input

输入包含不超过 30 组数据。
每组数据包含两个整数 n,m (1≤n,m≤109).

Output

对于每组数据,输出一个整数表示满足条件的数量。

Sample Input

32 632016 20161000000000 1000000000

Sample Output

1305767523146895502644
思路:
1、任何一个数都能表示成n=a*2016+i,m=b*2016+j;
 则n*m=a*b*2016^2+a*2016*j+b*2016*i+i*j;
如果i*j是2016的倍数的话,那么n*m一定是2016的倍数
若i*j == 0 先把a和b为2016的倍数的情况筛出出去——容斥方法
否则 在2015*2015的规模中遍历出符合i*j%2016=0的数,按照n中有多少个这样的i,m中有多少这样的j,然后相乘,累加起来就得到的区间中所有的数
Select Code#include <iostream>#include <cstdio>#include <algorithm>using namespace std;int main(){    long long n,m;    while(cin >> n>> m)    {        long long ans = (n/2016)*m + (m/2016)*n - (n/2016)*(m/2016);        for(int i = 1; i <= min(n,(long long) 2015);i++)            for(int j = 1; j <= min(m,(long long) 2015);j++)        {            if((i*j)%2016 == 0)                ans += ((n-i)/2016 + 1)*((m-j)/2016 + 1);        }        cout << ans <<endl;    }    return 0;}



原创粉丝点击