LightOJ 1014 - Ifter Party (**求因子)

1014 - Ifter Party
Time Limit: 2 second(s)Memory Limit: 32 MB

I have an Ifter party at the 5th day ofRamadan for the contestants. For this reason I have invitedCcontestants and arranged P piaju's (some kind of food, specially madefor Ifter). Each contestant ateQ piaju's and L piaju's were left(L < Q).

Now you have to find the number of piaju's each contestantate.

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 ofpossible integers in ascending order. If no such integer is found print'impossible'.

Sample Input

Output for Sample Input

4

10 0

13 2

300 98

1000 997

Case 1: 1 2 5 10

Case 2: 11

Case 3: 101 202

Case 4: impossible

 



题意:有C个人,然后给他们P个食物,每个人吃Q个,然后剩下L个,求Q可能的情况


思路:其实一看直接暴力枚举就行了,复杂度sqrt(n),但是这道题岂会那么简单,如果枚举过后直接sort定会TLE,所以说要姿势了,将能整除的存起来,然后后面判断,倒序判断,加上中间的剪枝,终于过了。。。


总结:各种TLE,优先队列,小数组sort什么的都用上了,还是TLE,无语。。然后就xjb搞搞出来个这个方法,过了。



ac代码:

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 1001000
#define LL long long
#define ll __int64
#define INF 0x7fffffff
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
#define eps 1e-10
using namespace std;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int 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;}
//head
int ans[MAXN];
int main()
{
    int t,p,l,i;
    int cas=0;
    scanf("%d",&t);
    while(t--)
    {
    	
        scanf("%d%d",&p,&l);
        int num=p-l;
        int bz=0;
		int cnt=0;
        printf("Case %d:",++cas);
        if(num<=l)
        {
            printf(" impossible\n");
            continue;
        }
        for(i=1;i<=(int)sqrt(num);i++)
        {
            if(num%i==0)
            {
            	ans[cnt++]=i;
                if(i>l)
                bz=1;
                if(num/i>l)
                bz=1;
            }
        }
        if(bz==0)
        {
            printf(" impossible\n");
            continue;
        }
        for(i=0;i<cnt;i++)
        if(ans[i]>l)
        printf(" %d",ans[i]);
        if(ans[cnt-1]*ans[cnt-1]==num)
        cnt--;
        for(i=cnt-1;i>=0;i--)
        if(num/ans[i]>l)
        printf(" %d",num/ans[i]);
        printf("\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值