题目大意:
节日到了,家里办party,邀请了c个小伙伴来做客,家里有P个派每个小伙伴吃掉Q个派,剩下L个,输入P、L,要求你输出所有的可能的Q的值;
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 |
P and L (0 ≤ L < P < 231).
解题思路:
假设n=P-L,那么这道题目的意思就是要你求出所有的n的因数啊!其实直接暴力枚举就到了,因为时间复杂度为n
上个代码:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#define __int64 long long
using namespace std;
int cas;
int P,L,n;
void Ans ()
{
set<int> s;//用set()既不用考虑重复,又不用考虑排序,多么美好的一个东西啊。
for (__int64 i=1;i*i<=n;i++)
if (n%i == 0)
{
if (i>L)
s.insert(i);
if (n/i > L)
s.insert(n/i);
}
printf("Case %d: ",cas);
if (s.empty())
printf("impossible\n");
else
{
set<int>::iterator it,it2;
it2=s.begin();
it2++;
for (it=s.begin();it2!=s.end();it++,it2++)
printf("%d ",*it);
printf("%d",*it);
printf("\n");
}
}
int main()
{
int T;
scanf("%d",&T);
for (cas=1;cas<=T;cas++)
{
scanf("%d%d",&P,&L);
n=P-L;
Ans ();
}
return 0;
}