题目来源:http://lightoj.com/volume_showproblem.php?problem=1014
计算P-L的因子中大于L的数的个数,然后求因子的时候顺便就储存起来,利用set就非常方便!
#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
using namespace std;
int main()
{
set <int> iSet;
__int64 i;
int p, l, T, n, kcase = 1;
scanf("%d", &T);
while(T--)
{
iSet.clear();
scanf("%d %d", &p, &l);
n = p-l;
for(i = 1; i*i <= n; ++i)
{
if(n%i == 0)
{
if(i > l)
iSet.insert(i);
if(n/i > l)
iSet.insert(n/i);
}
}
if(iSet.empty())
{
printf("Case %d: impossible\n", kcase++);
continue ;
}
printf("Case %d: ", kcase++);
set <int>::iterator it, it1;
it1 = it = iSet.begin();
it1++;
for(; it1 != iSet.end(); ++it, ++it1)
printf("%d ", *it);
printf("%d\n", *it);
}
return 0;
}