The Bad Number poj3902
来源:互联网 发布:淘宝模板制作 编辑:程序博客网 时间:2024/06/05 17:07
Language:
The Bad Number
Description
John and Brus believe that number N is a very bad number. Thus they try to avoid it every time and everywhere.
Now the guys would like to represent number M as a sum of positive numbers, each of which not exceeding K. But don’t forget about the bad number N! Each summand must not be divisible by N, moreover the number of summands also must not be divisible by N.
Your task is to find the minimal possible number of summands in such representation of M.
For example, if N=3, M=11, K=6 then we can represent M as 5+6, but as far as 6 is divisible by 3 we must have at least 3 summands. But as far as N=3 we can’t have 3 summands and thus the answer is 4. One of the possible ways to represent M is 11=4+4+2+1.
Now the guys would like to represent number M as a sum of positive numbers, each of which not exceeding K. But don’t forget about the bad number N! Each summand must not be divisible by N, moreover the number of summands also must not be divisible by N.
Your task is to find the minimal possible number of summands in such representation of M.
For example, if N=3, M=11, K=6 then we can represent M as 5+6, but as far as 6 is divisible by 3 we must have at least 3 summands. But as far as N=3 we can’t have 3 summands and thus the answer is 4. One of the possible ways to represent M is 11=4+4+2+1.
Input
The first line contains single integer T – the number of test cases. Each test case consists of a single line containing three integers N, M and K separated by single spaces.
Output
For each test case print a single line containing the minimal possible number of summands according to the requirements described above. If it is impossible to do this output “-1” (quotes for clarity) instead.
Sample Input
2 3 11 6 2 12 47
Sample Output
4 -1
Hint
Constraints:
1 ≤ T ≤ 74,
1 ≤ N, M, K ≤ 1000000000 (109).
1 ≤ T ≤ 74,
1 ≤ N, M, K ≤ 1000000000 (109).
Source
Southeastern European Regional Programming Contest 2009
//数论好难,几天,都没有思路,#include <stdio.h>#include <iostream>using namespace std;int main (){ int t,n,m,k,last,answer,temp,flag,num,i; scanf("%d",&t); while(t--) { scanf("%d%d%d",&n,&m,&k); if(k%n==0)k--;//如果k是n的倍数,最大值就不能取k了 if(n==1) { printf("-1\n"); } else if(k==1) { if(m%n==0)//k为1只能分成m个 { printf("-1\n"); } else { printf("%d\n",m); } } else if(n==2) { if(m%n==0)//m是偶数,要么分解的个数是偶数个,要么分解的数含偶数排除了去 { printf("-1\n"); } else { answer=m/k; last=m%k; if(last>0) { answer++; if(last%2==0) answer++; } printf("%d\n",answer); } } else{ num=answer=m/k; last=m%k; if(last>0) { answer++; if(answer%n==0) { answer++; } else if(last%n==0) { if(answer==1) answer++; else if((k-1)%n==0) { flag=0; for(i=0;i<n&&i+last<=k&&i<k;i++) { if((last+i)%n!=0&&(k-i)%n!=0) { flag=1; break; } } if(!flag) { answer++; temp=0; if(answer%n==0) { temp+=last-1; if(temp>=2)answer++; } else if(temp==1) { if((k-1)%n!=0) answer++; else if((k-2)<=0) answer=-1; else answer++; } } } } } else if(answer%n==0) answer++; printf("%d\n",answer); } } return 0;}
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator
- POJ3902 The Bad Number
- The Bad Number poj3902
- POJ 3902 The Bad Number(贪心)
- POJ 3902 The Bad Number第一次积分赛K题
- POJ 3902 The Bad Number 已被翻译
- 好贴要转--bad magic number
- Bad number of channels()
- ZOJ3697-Bad-written Number
- ZOJ3697 Bad-written Number
- Bad version number in .class.....
- ZOJ 3697 Bad-written Number
- ZOJ 3697 Bad-written Number
- OGG-01224 Bad file number
- TortoiseGit Bad file number 解决方法
- Git的Bad file number
- The bad news
- the good,the bad ,the ugly
- Are the Singleton Really Bad
- http://subject.csdn.net/sohu/
- 大型互联网算法面试题
- C语言中的字符串
- Python100例——第七章----逻辑推理与判断
- mongodb编译
- The Bad Number poj3902
- iOS开发——加载、滑动翻阅大量图片解决方案详解
- 手机号码归属地查询api接口
- BIOS Usb 3.0 Key Behavior In Deep S3
- 动态顺序统计
- 使用Pushlet实现后台信息推送(一)
- Objective-C 和 Core Foundation 对象相互转换的内存管理总结
- 浅谈Linux PCI设备驱动(一)
- 【转】麻瓜项目:家庭能源消耗管理之“网络电能表”(乐联网有线网络版