「POJ3696」The Luckiest number【数论,欧拉函数】

# 题解

一道数论欧拉函数和欧拉定理的入门好题。
虽然我提交的时候POJ炸掉了,但是在hdu里面A掉了,应该是一样的吧。
首先我们需要求的这个数一定可以表示成\(\frac{(10^x-1)}{9}\times 8\)。
那么可以列出一个下面的方程
\[\frac{(10^x-1)}{9}\times 8=L\times k\]

设\(d=gcd(9L,8)=gcd(L,8)\)

\[\frac89(10^x-1)=Lk\]

\[\frac{8(10^x-1)}d=\frac{9Lk}{d}\]

令\(p=\frac8d,q=\frac{9L}d\),易证\(p\)和\(q\)互质。

\[p(10^x-1)=qk\]

可得\(q|10^x-1\),所以得到了\(10^x\equiv1(mod \ q)\)

根据欧拉定理,当\(10\)和\(q\)互质,必定有一组解,是\(\varphi(q)\)

那么最小的一组解一定是\(\varphi(q)\)的一个约数。



那么欧拉函数计算一下,然后枚举一下约数,快速幂判断一下就好了。

代码

#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
#define ll long long
#define db double
using namespace std;
ll L, ans;
bool fg;
ll gcd(ll x, ll y) { return y == 0 ? x : gcd(y, x % y) ; }
ll mulmod(ll x, ll y, ll mod) {
    ll res = 0ll;
    for (; y; y >>= 1) { if (y & 1) res = (res + x) % mod; x = (x << 1) % mod; }
    return res;
}
ll power(ll x, ll y, ll mod) {
    ll res = 1ll;
    for (; y; y >>= 1) { if (y & 1) res = mulmod(res, x, mod); x = mulmod(x, x, mod); }
    return res;
}
ll euler(ll x) {
    ll res = x;
    for (ll i = 2; i * i <= x; i ++) {
        if (x % i == 0) {
            res = res / i * (i - 1);
            while (x % i == 0) x /= i;
        }
    }
    if (x > 1) res = res / x * (x - 1);
    return res;
}
int main() {
    int cas = 0;
    while (~scanf("%I64d", &L) && L) {
        ll d = gcd(L, 8), q = 9 * L / d; fg = 0;
        if (gcd(q, 10) != 1) printf("Case %d: 0\n", ++ cas);
        else {
            ll phi = euler(q), m = sqrt((db)(phi));
            ans = phi;
            for (int i = 1; i <= m; i ++)
                if (phi % i == 0 && power(10, i, q) == 1) { ans = i; fg = 1; break; }
            if (!fg) for (int i = m; i >= 2; i --) {
                if (phi % i == 0 && power(10, phi / i, q) == 1) { ans = phi / i; break; }
            }
            printf("Case %d: %I64d\n", ++ cas, ans);
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/chhokmah/p/10695998.html

时间: 2024-08-07 08:36:00

「POJ3696」The Luckiest number【数论,欧拉函数】的相关文章

HDU 4002 Find the maximum(数论-欧拉函数)

Find the maximum Problem Description Euler's Totient function, φ (n) [sometimes called the phi function], is used to determine the number of numbers less than n which are relatively prime to n . For example, as 1, 2, 4, 5, 7, and 8, are all less than

POJ 2154 Color(组合数学-波利亚计数,数论-欧拉函数,数论-整数快速幂)

Color Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7693   Accepted: 2522 Description Beads of N colors are connected together into a circular necklace of N beads (N<=1000000000). Your job is to calculate how many different kinds of th

欧拉函数性质与求法 [数论][欧拉函数]

n的欧拉函数值用符号φ(n)表示 欧拉函数的定义是,对于一个正整数n,小于n且与n互质的数的数目(包括1,特殊地,φ(1)=1 ). 设p1,p2,p3,...,pr为n的全部r个质因数,则有φ(n)=n*(1-1/p1)*(1-1/p2)*(1-1/p3)*(1-1/p4)…..(1-1/pr). 显然,用这个方法来计算单个欧拉函数是可以求解的. 附上代码: 1 int get_phi(int x){ 2 int re=x; 3 for(int i=2;i*i<=x;i++) 4 if(x%i

hdu1395 数论 欧拉函数

hdu1395 数论   欧拉函数对于给出的每一个n 求最小正整数 x 满足 2^x mod n = 1 1.如果给出的n 是偶数或者 1 则一定无解2.如果是奇数 首先根据欧拉定理 我们可知 phi(n)一定是满足要求的 然后答案一定是 phi( i ) 的因数 然后我们就可以 O(sqrt(phi(i))的时间内 枚举每个因数 然后快速幂验证就行了 1 #include <bits/stdc++.h> 2 using namespace std ; 3 4 const double eps

bzoj 4026 dC Loves Number Theory (主席树+数论+欧拉函数)

题目大意:给你一个序列,求出指定区间的(l<=i<=r) mod 1000777 的值 还复习了欧拉函数以及线性筛逆元 考虑欧拉函数的的性质,(l<=i<=r),等价于 (p[j]是区间内所有出现过的质数) 那么考虑找出区间内所有出现过的质数,这思路和HH的项链是不是很像?? 由于此题强制在线,所以把树状数组替换成了主席树而已 原来我以前写的主席树一直都是错的......还好推出了我原来错误代码的反例 在继承上一个树的信息时,注意不要破坏现在的树 1 #include <cs

HDU1695-GCD(数论-欧拉函数-容斥)

GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5454    Accepted Submission(s): 1957 Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y

数论 - 欧拉函数的运用 --- poj 3090 : Visible Lattice Points

Visible Lattice Points Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5636   Accepted: 3317 Description A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal to 0), other than the origin, is visible fr

数论-欧拉函数

题目1 : 数论五·欧拉函数 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho有时候会用密码写信来互相联系,他们用了一个很大的数当做密钥.小Hi和小Ho约定了一个区间[L,R],每次小Hi和小Ho会选择其中的一个数作为密钥. 小Hi:小Ho,这次我们选[L,R]中的一个数K. 小Ho:恩,小Hi,这个K是多少啊? 小Hi:这个K嘛,不如这一次小Ho你自己想办法算一算怎么样?我这次选择的K满足这样一个条件: 假设φ(n)表示1..n-1中与n互质的数的个

数论&#183;欧拉函数

欧拉函数$phi(n)$表示不超过$n$的正整数中与$n$互质的个数,并且有: $\varphi(n)= n\sum\limits_{p|n}(1-{\frac 1{p}})$ 显然有若$n$素数: $\varphi(n)=n-1$ 并且考虑$mp$,若$p$为素数,则对任意整数$k$: $(mp, k)\Leftrightarrow (m, k)$ 于是在每个模$p$的剩余系中有$\varphi(m)$个数与$mp$互质,因此: $\varphi(mp)=\varphi(m)\varphi(p