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

来源:互联网 发布:路旁的叶修写真集淘宝 编辑:程序博客网 时间:2024/06/05 14:14
1014 - Ifter Party
 PDF (English)StatisticsForum
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 arrangedP piaju's (some kind of food, specially madefor Ifter). Each contestant ateQ piaju's andL 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-10using 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;}//headint 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
原创粉丝点击