您的位置:首页 > 其它

Codeforces Round #282 (Div. 2) B.(数学)

2015-01-07 17:47 253 查看
题目链接:http://codeforces.com/contest/495/problem/B

解题思路:

求a % x == b,可以得知(a - b) % x == 0,x > b。

当a == b时,一定有无穷多解;当a < b时,输出0;其余情况输出(a - b)的所有因子。这里加一个优化,每次只扫 i ,然后分别判断 i 和 a -b是否满足大于b的情况 。

完整代码:

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <climits>
#include <cassert>
#include <complex>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;

#pragma comment(linker, "/STACK:102400000,102400000")

typedef long long LL;
typedef double DB;
typedef unsigned uint;
typedef unsigned long long uLL;

/** Constant List .. **/ //{

const int MOD = int(1e9)+7;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
const DB EPS = 1e-9;
const DB OO = 1e20;
const DB PI = acos(-1.0); //M_PI;

int main()
{
    #ifdef DoubleQ
    freopen("in.txt","r",stdin);
    #endif
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int a , b;
    while(cin >> a >> b)
    {
        if(a == b)
        {
            cout << "infinity" << endl;
            continue;
        }
        else if(a < b)
        {
            printf("0\n");
            continue;
        }
        int cnt = 0;
        int k = a - b;
        int i;
        for(i = 1 ; i * i < k ; i ++)
        {
            if(k % i == 0)
            {
                if(i > b)
                    cnt ++;
                if(k / i > b)
                    cnt ++;
            }
        }
        if(i * i == k && i > b)
            cnt ++;
        printf("%d\n",cnt);
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: