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
大致题意:就是给你两个数正整数P和L,让你求P-L的值的所有大于L的约数并从小到大输出。
思路:暴力枚举下求出它的约数,时间复杂度为sqrt(n),然后将所求得约数存入set中,输出的时候直接输出即可。
代码如下
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned long long
int main()
{
int T;
scanf("%d",&T);
for(int cas=1;cas<=T;cas++)
{
set<int> num;
set<int>::iterator it;
int P,L;
scanf("%d%d",&P,&L);
int a=P-L;
int f=0;
printf("Case %d:",cas);
if(L+1>a)
printf(" impossible");
else
for(int i=1;i<=(int)sqrt(a);i++)
{
if(a%i==0)
{
if(i>L)
num.insert(i);
if(a/i>L)
num.insert(a/i);
}
}
for(it=num.begin();it!=num.end();it++)
{
printf(" %d",*it);
}
printf("\n");
}
return 0;
}