码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-04-09 20:41:05      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:mat   pac   style   输入   template   整数   fine   cto   pen   

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

 

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

标签:mat   pac   style   输入   template   整数   fine   cto   pen   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!