HDU 4937 lucky number (number theory)

Source: Internet
Author: User
HDU 4937 lucky number

Question Link

Assume that a number is given and X is given. Each hexadecimal digit has only 3, 4, 5, and 6. determine the number of X types. If there are infinite types of output-1

Idea: first, the 3, 4, 5, and 6 characters are unlimitedly determined, and it is easy to think that it is not proved. Then, the last digit of the enumerated number is 3, 4, 5, 6, then the hexadecimal number must be a factor from this number, because the remaining number must be a1x ^ 1 + a2x ^ 2 + a3x ^ 3... in this way, you only need to find the base in the factor to determine. The method for finding the factor is decomposed first and then DFS is searched. The time-out is returned when the direct try-out is performed.

Code:

#include <cstdio>#include <cstring>#include <cmath>#include <set>#include <algorithm>using namespace std;typedef long long ll;const ll N = 1e6 + 5;int t, vis[N], pn = 0, cnt[N], fn;ll n, prime[N], fra[N];set<ll> ans;void getFra(ll n) {    fn = 0;    for (int i = 0; i < pn && n >= prime[i]; i++) {if (n % prime[i] == 0) {    fra[fn] = prime[i];    cnt[fn] = 0;    while (n % prime[i] == 0) {n /= prime[i];cnt[fn]++;    }    fn++;}    }    if (n != 1) {fra[fn] = n;cnt[fn++] = 1;    }}bool check(ll b) {    ll tmp = n;    while (tmp) {if (tmp % b < 3 || tmp % b > 6)    return false;tmp /= b;    }    return true;}void dfs(int now, ll sum) {    if (now == fn) {if (check(sum))    ans.insert(sum);return;    }    ll tmp = 1;    for (int i = 0; i <= cnt[now]; i++) {dfs(now + 1, sum * tmp);tmp *= fra[now];    }}void solve(ll n, ll bas) {    getFra(n);    dfs(0, 1);}bool judge(ll n) {    if (n == 3 || n == 4 || n == 5|| n == 6)return true;    return false;}int main() {    for (ll i = 2; i < N; i++) {if (vis[i]) continue;prime[pn++] = i;for (ll j = i * i; j < N; j += i)    vis[j] = 1;    }    int cas = 0;    scanf("%d", &t);    while (t--) {scanf("%I64d", &n);if (judge(n)) {    printf("Case #%d: -1\n", ++cas);    continue;}ans.clear();for (ll i = 3; i <= 6; i++)    solve(n - i, i);int out = ans.size();printf("Case #%d: %d\n", ++cas, out);    }    return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.