LightOJ 1014 Ifter Party (因子分解)

Ifter Party

I have an Ifter party at the 5th day of Ramadan for the contestants. For this reason I have invited C contestants and arranged P piaju’s (some kind of food, specially made for Ifter). Each contestant ate Q piaju’s and L piaju’s were left (L < Q).

Now you have to find the number of piaju’s each contestant ate.

Input
Input starts with an integer T (≤ 325), denoting the number of test cases.

Each case contains two non-negative integers P and L (0 ≤ L < P < 231).

Output
For each case, print the case number and the number of possible integers in ascending order. If no such integer is found print ‘impossible’.

Sample Input

4
10 0
13 2
300 98
1000 997

Sample Output

Case 1: 1 2 5 1
Case 2: 11
Case 3: 101 202
Case 4: impossible

题意
给出一个食物总数量 p p p,邀请不定数量的人吃并且每个人吃的数量是一样的,最后剩余 l l l的食物。求每个参赛者吃的食物数量。

题解
n = p − l n=p-l n=pl,求 n n n的因子,我们要的是大于 l l l的因子。暴力跑就可以,直接用set省的排序了,甚至避免了加两次 n \sqrt{n} n

代码

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <cmath>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <math.h>
#include <unordered_map>
#include <tr1/unordered_map>

using namespace std;
#define me(x,y) memset(x,y,sizeof x)
#define MIN(x,y) x < y ? x : y
#define MAX(x,y) x > y ? x : y

typedef long long ll;
typedef unsigned long long ull;

const int maxn = 1e6;
const int INF = 0x3f3f3f3f;
const int MOD = 998244353;
const double eps = 1e-06;
const double PI = acos(-1);

int main(){
    int t;
    int ca = 1;
    cin>>t;
    while(t--){
        int p,l;
        set<int>s;
        scanf("%d%d",&p,&l);
        int n = p-l;
        for(ll i = 1; i*i <= n; ++i){
            if(n%i==0){
                if(i > l) s.insert((int)i);
                if(i*i != n && n/i > l) s.insert((int)n/i);
            }
        }
        printf("Case %d:",ca++);
        if(s.empty()){
            printf(" impossible\n");
            continue;
        }
        set<int>::iterator it;
        for(it = s.begin(); it != s.end(); it++){
            printf(" %d",*it);
        }
        printf("\n");
    }    
    return 0;
}

/*

*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值