CSU 1803 - 2016 - [同余]

题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1803

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

Input

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

Output

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

Sample Input

32 63
2016 2016
1000000000 1000000000

Sample Output

1
30576
7523146895502644
 
题解:
根据 (a * b) mod 2016 = [(a mod 2016) * (b mod 2016)] % 2016,
我们可以求出amo[1~2016][1~2016](也就是说当n≤2016且m≤2016时的问题答案);
然后对于n>2016或者m>2016的情况,那么就把 n 变成 k1*2016+g1,m 变成 k2*2016+g2 的形式;
那么他们互相组合就可以得到 amo[2016][2016]*k1*k2 + amo[g1][2016]*k2 + k1*amo[2016][g2] + amo[g1][g2].
 
AC代码:
#include<cstdio>
using namespace std;
typedef long long LL;

LL n,m;
LL amo[2020][2020];
void init()
{
    LL sum;
    amo[0][0]=0;
    for(int i=1;i<=2016;i++)
    {
        amo[i][0]=amo[0][i]=0;
        sum=0;
        for(int j=1;j<=2016;j++)
        {
            if((i*j)%2016==0) sum++;
            amo[i][j]=amo[i-1][j]+sum;
        }
    }
}

int main()
{
    init();
    while(scanf("%lld%lld",&n,&m)!=EOF)
    {
        LL a1=n/2016, b1=n%2016;
        LL a2=m/2016, b2=m%2016;
        printf("%lld\n",amo[2016][2016]*a1*a2+a1*amo[2016][b2]+a2*amo[b1][2016]+amo[b1][b2]);
    }
}

注意:CSU的OJ不能使用bits/stdc++.h,同时注意数字范围超int,需要使用long long.

posted @ 2018-05-02 21:54  Dilthey  阅读(302)  评论(0编辑  收藏  举报