HDU - 4937 Lucky Number,数论(枚举+进制转化)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sdz20172133/article/details/86592850

Lucky Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1936    Accepted Submission(s): 590


 

Problem Description

“Ladies and Gentlemen, It’s show time! ”

“A thief is a creative artist who takes his prey in style... But a detective is nothing more than a critic, who follows our footsteps...”

Love_Kid is crazy about Kaito Kid , he think 3(because 3 is the sum of 1 and 2), 4, 5, 6 are his lucky numbers and all others are not. 

Now he finds out a way that he can represent a number through decimal representation in another numeral system to get a number only contain 3, 4, 5, 6. 

For example, given a number 19, you can represent it as 34 with base 5, so we can call 5 is a lucky base for number 19. 

Now he will give you a long number n(1<=n<=1e12), please help him to find out how many lucky bases for that number. 

If there are infinite such base, just print out -1.

 

Input

There are multiply test cases.

The first line contains an integer T(T<=200), indicates the number of cases. 

For every test case, there is a number n indicates the number.

 

Output

For each test case, output “Case #k: ”first, k is the case number, from 1 to T , then, output a line with one integer, the answer to the query.

 

Sample Input

 

2 10 19

 

Sample Output

 

Case #1: 0 Case #2: 1

Hint

10 shown in hexadecimal number system is another letter different from ‘0’-‘9’, we can represent it as ‘A’, and you can extend to other cases.

 

Author

UESTC

 

Source

2014 Multi-University Training Contest 7

 

Recommend

We have carefully selected several similar problems for you:  6460 6459 6458 6457 6456 

题意:

给你一个十进制的数字n,然后问你转化为某一进制后它的每一位的数字只可能为3,4,5,6.求这种符合条件的进制有多少种。无穷多输出-1.

分析:

我们枚举n转化为某进制时候有几位数。

对于一位的状况,3,4,5,6明显是-1

对于二位,解方程a*x+b=n

对于三位,解方程a*x^2+b*x+c=n

其他的从4进制开始枚举即可

 

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#define LL long long
using namespace std;
int kcase = 1;
bool judge(LL base, LL n)
{
    while(n)
    {
        if(n % base < 3 || n % base > 6)
            return false;
        n /= base;
    }
    return true;
}
void solve()
{
    LL n;
    scanf("%lld", &n);
    printf("Case #%d: ", kcase++);
    if(n >= 3 && n <= 6)
    {
        printf("-1\n");
        return ;
    }
    LL ans = 0;
    for(LL a = 3; a <= 6; a++)
    {
        for(LL b = 3; b <= 6; b++)
        {
            if((n-a) % b) continue;
            if((n-a) / b > max(a, b))
                ans++;
        }
    }
    for(LL c = 3; c <= 6; c++)
    {
        for(LL b = 3; b <= 6; b++)
        {
            for(LL a = 3; a <= 6; a++)
            {
                LL C = c - n;
                double temp = sqrt(b*b - 4*a*C);
                if(temp == (LL)temp)
                {
                    LL d = (LL)temp;
                    if((d-b) % (2*a) == 0)
                    {
                        LL x = (d-b) / (2*a);
                        if(x > max(max(a, b), c))
                            ans++;
                    }
                }
            }
        }
    }
    for(LL base = 2; base * base * base <= n; base++)
        if(judge(base, n))
            ans++;
    printf("%lld\n", ans);
}
int main()
{
    int t;
    scanf("%d", &t);
    while(t--){
        solve();
    }
    return 0;
}

这个代码一直WA,不知道为什么

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<set>
#include<queue>
#include<stack>
#include<map>
using namespace std;
typedef long long ll;
const int N=1e5+5;
ll n,m;

   
 
int main() {
   

    int t;
    scanf("%d",&t);
    int kase=0;
    while(t--)
	{  
	   scanf("%lld",&n);
	   if(n>=3&&n<=7)
	   {
	   	  printf("Case #%d: -1\n",++kase);
	   	continue;
	   }
	   ll ans=0;
	   ///2位数
	   for(ll i=3;i<=6;i++)
	   	for(ll j=3;j<=6;j++)
	   {
	   	if((n-j)%i==0)
		{
			ll temp=(n-j)/i;
			if(temp>max(i,j))
				ans++;
		}
	   }
	   ///三位数
	   for(ll i=3;i<=6;i++)
	   	for(ll j=3;j<=6;j++)
	   	  for(ll k=3;k<=6;k++)
	   {
	      	ll c=k-n;
	   	    double d=sqrt(j*j-4*i*c);
	   	    if(d==(ll)d)
			{
			   if(((ll)d-j)%(2*i)==0)
			   {
			   	ll x=((ll)d-j)/(2*i);
			   	 if(x>max(max(i,j),k))
				 {
				 	ans++;
				 }
			   }
			}
	   }
	   ///其他位数
	   for(ll i=2;i*i*i<=n;i++)///枚举所有进制
	   {
	   	ll tt=n;
	   //	int flag=1;
	   	while(tt)
		{
			if(tt%i<3||tt%i>6)
			{
				//flag=0;
				break;
			}
			tt/=i;
		}
		if(tt==0)
		{
			ans++;
		}
	   }
	
	    printf("Case #%d: %lld\n",++kase,ans);
	   
	}
    return 0;
}
 
 
 
 
 
 
 
 
 
 
 
 

猜你喜欢

转载自blog.csdn.net/sdz20172133/article/details/86592850