现在的位置: 首页 > 综合 > 正文

The Bad Number poj3902

2013年09月11日 ⁄ 综合 ⁄ 共 2078字 ⁄ 字号 评论关闭
         
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 (109).

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

抱歉!评论已关闭.