LightOJ 1014 - Ifter Party (**求因子)
2016-02-05 22:40
609 查看
1014 - Ifter Party
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.
Each case contains two non-negative integers P and L(0 ≤ L < P < 231).
题意:有C个人,然后给他们P个食物,每个人吃Q个,然后剩下L个,求Q可能的情况
思路:其实一看直接暴力枚举就行了,复杂度sqrt(n),但是这道题岂会那么简单,如果枚举过后直接sort定会TLE,所以说要姿势了,将能整除的存起来,然后后面判断,倒序判断,加上中间的剪枝,终于过了。。。
总结:各种TLE,优先队列,小数组sort什么的都用上了,还是TLE,无语。。然后就xjb搞搞出来个这个方法,过了。
ac代码:
PDF (English) | Statistics | Forum |
Time Limit: 2 second(s) | Memory Limit: 32 MB |
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; }
相关文章推荐
- CentOS环境下Redis安装
- SVN 分支管理
- 2016-2-5 linux基础学习11
- html需要掌握的几个全局属性
- bzoj2120 数颜色 分块
- Java 集合类
- int 转换成 CString(VC2008里有这个问题)
- POJ3694 Network(边双连通+LCA)
- pycharm 常用设置
- mysql_real_escape_string总是返回false
- 爬虫基本知识
- JavaScript的this分别代表什么
- 用户爬虫例子
- 开源爬虫
- 终于懂了:Delphi消息的Result完全是生造出来的,不是Windows消息自带的(Delphi对Windows编程体系的改造越大,学习收获就越大)
- 【HDOJ】3686 Traffic Real Time Query System
- HDU——1019Least Common Multiple(多个数的最小公倍数)
- Yaf安装及配置
- java中形参长度可变的方法
- 多态