您的位置:首页 > 其它

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

2016-02-05 22:40 609 查看
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 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: