第十二届湖南省赛 A - 2016 ( 数学,同余转换)

给出正整数 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

Hint

思路:

1.由于给定的数据很大,所以不可以暴力求解。

2.我们应该知道这样的公式:

  (a*b)%c == ( (a%c) * (b%c) )%c

那么我们的本题对应公式就是c=2016,

a和b在1~n和1~m这两个区间分别取。

则我们可以知道,a*b 是2016的倍数就是 (A*B)%C == 0

那么我们可以转换为1~n中的A在 A%2016中分别有多少个数,然后我们再枚举 1~2016 * 1~2016 哪些是2016的倍数,答案加上对应的组合数量即可。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), ‘\0‘, sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
ll n,m;
ll numn[maxn];
ll numm[maxn];
int main()
{
//    freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
    //freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
    gbtb;
    while(cin>>n>>m)
    {
        ll ans=0ll;
        repd(i,1,2016)
        {
            numn[i]=n/2016;
        }
        repd(i,1,2016)
        {
            numm[i]=m/2016;
        }
        repd(i,1,(n%2016))
        {
            numn[i]++;
        }
        repd(i,1,(m%2016))
        {
            numm[i]++;
        }
        repd(i,1,2016)
        {
            repd(j,1,2016)
            {
                if((i*j)%2016==0)
                {
                    ans+=numn[i]*numm[j];
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ‘ ‘ || ch == ‘\n‘);
    if (ch == ‘-‘) {
        *p = -(getchar() - ‘0‘);
        while ((ch = getchar()) >= ‘0‘ && ch <= ‘9‘) {
            *p = *p * 10 - ch + ‘0‘;
        }
    }
    else {
        *p = ch - ‘0‘;
        while ((ch = getchar()) >= ‘0‘ && ch <= ‘9‘) {
            *p = *p * 10 + ch - ‘0‘;
        }
    }
}

原文地址:https://www.cnblogs.com/qieqiemin/p/10679279.html

时间: 2024-08-29 02:25:28

第十二届湖南省赛 A - 2016 ( 数学,同余转换)的相关文章

2016湖南省赛----A 2016 (同余定理)

2016湖南省赛----A 2016 (同余定理) Description 给出正整数 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

第十二届湖南省省赛总结

首先,要祝贺一下本校 hunau 创历史新高,然后有一金二银一铜,我们队很悲催的排在了二等第一..... 今天的题目一上场就感觉较往年的题难度变高了,没有水题,感觉自从湘潭赛以后整体难度变高了,湖南省难度想逼近区域赛?估计都是这个趋势了,去年7个才一等,今年三个就行了. 然后我们上场先阅览了一遍题目,发现有个三角形和矩形面积交?刚刚好有模板,然后我立马上手敲,然后敲到最后发现第一份模板运行结果是错的,马上就换了一个队友上手敲另外一份,终于,当我们最出这个题的时候,已经1h+了,然后马上看Rank

CSU 1511 残缺的棋盘 第十届湖南省赛题

题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1511 题目大意:在一个8*8的棋盘中,给你一个起点位置和一个终点位置,同时也给你一个陷阱位置,问你从起点绕过陷阱到终点的最短距离.(可以上下左右走还可以斜走) 解题思路:可以直接用搜索,也可以用数学知识来解决,我们之前学过,两点之间直接最短,所以当陷阱不在这条直线上的时候,我们就不用考虑陷阱了,直接是max(abs(x1-y1),abs(x2-y2))最终结果, 但是如果陷阱在两条直线

CSU 1507 超大型LED显示屏 第十届湖南省赛题

题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1507 解题思路:这是一道模拟题,看了那么多人的代码,我觉得我的代码是最简的,哈哈,其实就是分数变幻的时候要计算灯管的亮数复杂一点,我就直接暴力咯 AC代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std;

浙江省第十二届省赛 Beauty of Array

Description Edward has an array A with N integers. He defines the beauty of an array as the summation of all distinct integers in the array. Now Edward wants to know the summation of the beauty of all contiguous subarray of the array A. Input There a

Little Sub and Traveling(杭师大第十二届校赛E题) 欧拉回路

题目传送门 题目大意: 从0出发,每次只能跳到(i*2)%n或者(i*2+1)%n,求字典序最大的哈密顿回路. 思路: 首先n为奇数时无解,先来证明这一点. 先假设n为奇数,若要回到原点,则必定有一步是$a%n=0$,则$a=kn(k为整数)$. 我们先假设a是通过$2x$的方式得到的,即$kn=2*x$,由于此时$n$为奇数,则k必定为偶数,由于$n>x$,所以$k<2$,k又不能等于0,所以此时k无解,a不是通过$s*x$的方法得到的 那么$kn=2*x+1$,k为奇数,则$k=1  ,a

2016年湖南省第十二届大学生计算机程序设计竞赛---Parenthesis(线段树求区间最值)

原题链接 http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 Description Bobo has a balanced parenthesis sequence P=p1 p2…pn of length n and q questions. The i-th question is whether P remains balanced after pai and pbi  swapped. Note that questions ar

(三角剖分)三角形和矩形的面积交 2016湖南省赛

1 // (三角剖分)三角形和矩形的面积交 2016湖南省赛 2 3 #include <iostream> 4 #include <cstdio> 5 #include <cstring> 6 #include <stack> 7 #include <queue> 8 #include <cmath> 9 #include <algorithm> 10 using namespace std; 11 12 const i

18年第十二届东北四省赛

18年第十二届东北四省赛 5.19晚上的东北四省赛的热身赛老师说不用参加,当时白天打了一场之后,也确实脑壳疼QAQ: 5.20很吉利的日期啊,9.30-2.30,还是一样的分工读题,外加看榜,然后很幸运,两道水题,一道A,一道K,队友直接听我们的上去敲,然后交的时候比较谨慎,0罚时,连A两题,然后挂机4小时40分钟?!然后我们讨论了B,然后就一直B....当时觉得B转换为二进制没问题,后来WA,发现有个地方爆Long long,然后又发现有个地方爆Long long,然后把乘法改成除法感觉不会爆