A-Left-Party(暴力法求因数个数)

题目传送门
A-Left-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 < 2^31).

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 10
Case 2: 11
Case 3: 101 202
Case 4: impossible

题目大意
问题简化就是总共Q件物品,被C个人拿走了一些后剩下L件物品。我们知道,每个人拿走的件数都大于L(相当于L就是Q被除某数后剩下的余数),假设拿走了X件物品,则X是等式Q(总数) - L(剩余数)所得出来数的因数,输出其所有的因数。
我刚刚开始做就是误判了以下误区:(处处是坑,==)
1.没理解清楚X>L;
2.虽然可以暴力求解,但sort排序超时;
3.int爆掉了。。。
4.也就是我现在AC的方法,将一个重复的数也输出了(下面代码里面可见注释)

#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int size = 4e5+6;
priority_queue<ll,vector<ll>,greater<ll> > q;
//优先队列设置题目从小到大输出的要求,提高输出效率,减少开销时间
int main(void)
{
    ll t,p,l,ch,kk=0;
    scanf("%lld",&t);
    while(t--)
    {
        ll flag=0;
        scanf("%lld %lld",&p,&l);
        ch = p-l;
        for(ll i=1;i<=(ll)sqrt(ch);i++) //找p-l的因数,也是折半二分吧==
        {
            if(ch%i==0)
            {
                if(i>l) q.push(i);
                if(ch/i > l && ch/i!=i) 
      //ch/i!=i这是千万不能少的,如果ch是25,i是5,那就会重复输出两个5
                    q.push(ch/i);
            }
        }
        if(q.size()>=1) flag=1;
        printf("Case %lld:",++kk);
        if(flag==0) printf(" impossible\n");
        else
        {
            while(q.size())
            {
                printf(" %lld",q.top());
                q.pop();
            }
            printf("\n");
        }
    }
}

拿下第一道ICPC题目(不过却是模拟题,还得努力昂);
B题传送门

原创文章 19 获赞 40 访问量 1万+

猜你喜欢

转载自blog.csdn.net/YSJ367635984/article/details/105469807