【HDU4937】x表示为n进制下特征数字的个数(质因数分解法)

Lucky Number

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


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

转自: http://blog.csdn.net/keshuai19940722/article/details/38519235

题目大意:给定一个数n,若这个数在base进制下全由3,4,5,6组成的话,则称base为n的幸运进制,给定n,求有多少个幸运进制。无穷多个的话输出-1,单个位置上超过9用相应的字符表示。

解题思路:首先n%base=3|4|5|6,假设我们当前考虑3,即n%base=3,那么就有:

  • n=basek+3
  • n3=basek

将枚举n-3的因子作为base,判断剩下的k是否也满足条件。注意这里枚举的因子要大于3,因为n%base=3.
将可行的base放入set中去重即可。
注意这里的无穷多解,只有在n=3|4|5|6的时候,因为对应34的话是一个字符,而不是现实34.

#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <list>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 0x3f3f3f3f
#define EPS 1e-6
#define TRUE true
#define FALSE false
typedef long long LL;
const double PI = acos(-1.0);
//#pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x)
{
    return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
#define N 1000005
int maxn = 1e6;
long long n;
set<long long >ans;
int np, vis[N], prime[N];
int c, cnt[N], fact[N];
long long nowbase;
void prime_table(int n)
{
    np = 0;
    memset(vis, 0, sizeof(vis));
    for (int i = 2; i <= n; i++)
    {
        if (vis[i])
            continue;
        prime[np++] = i;
        for (int j = i + i; j <= n; j += i)
            vis[j] = 1;
    }
}
void div_factor(long long n)
{
    c = 0;
    for (int i = 0; i < np && n >= prime[i]; i++)
    {
        if (n % prime[i] == 0)
        {
            cnt[c] = 0;
            fact[c] = prime[i];
            while (n % prime[i] == 0)
            {
                n /= prime[i];
                cnt[c]++;
            }
            c++;
        }
    }
    if (n != 1)
    {
        fact[c] = n;
        cnt[c++] = 1;
    }
}
bool judge(long long n, long long base)
{
    if (base <= nowbase)
        return false;
    while (n)
    {
        if (n % base < 3 || n % base > 6)
            return false;
        n /= base;
    }
    ans.insert(base);
    return true;
}
void dfs(int d, long long s, long long u)
{
    judge(u / s, s);
    if (d == c)
        return ;
    for (int i = 0; i <= cnt[d]; i++)
    {
        dfs(d + 1, s, u);
        s *= fact[d];
    }
}
void solve(long long n)
{
    if (n <= 0)
        return ;
    div_factor(n);
    dfs(0, 1, n);
}
int main()
{
#ifdef DeBUGs
    freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);
#endif
    prime_table(maxn);
    int cnt = 1;
    int T;
    scanf("%d", &T);
    while (T--)
    {
        scanf("%I64d", &n);
        printf("Case #%d: ", cnt++);
        if (n >= 3 && n <= 6)
            printf("-1\n");
        else
        {
            ans.clear();
            for (int i = 3; i <= 6; i++)
            {
                nowbase = i;
                solve(n - i);
            }
            printf("%d\n", ans.size());
        }
    }

    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值