The Bad Number poj3902

44 篇文章 0 订阅
     
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.

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 (10 9).

Source

//数论好难,几天,都没有思路,
#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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值