POJ 3696 The Luckiest Number 欧拉函数
2016-03-23 21:17
489 查看
是否存在n能整除的m使m各位都是8。
m=89(10x−1)=kn
8⋅10x−1gcd(8,n)=9⋅kngcd(8,n)
又gcd(8gcd(8,n),ngcd(8,n))=1
令q=9ngcd(8,n)
gcd(8gcd(8,n),q)=1
10x≡1modq
又aφ(p)≡1modp
的条件是gcd(a,p)=1
有gcd(10,q)=1时
10φ(q)≡1modq
不过φ(q)不是最小的,需要枚举φ(q)的因子并判断合法性。
似乎和原根求法有点像?
Total Submissions: 4884 Accepted: 1313
The last test case is followed by a line containing a zero.
11
16
0
Case 2: 2
Case 3: 0
m=89(10x−1)=kn
8⋅10x−1gcd(8,n)=9⋅kngcd(8,n)
又gcd(8gcd(8,n),ngcd(8,n))=1
令q=9ngcd(8,n)
gcd(8gcd(8,n),q)=1
10x≡1modq
又aφ(p)≡1modp
的条件是gcd(a,p)=1
有gcd(10,q)=1时
10φ(q)≡1modq
不过φ(q)不是最小的,需要枚举φ(q)的因子并判断合法性。
似乎和原根求法有点像?
#include <cstdio> #include <algorithm> using namespace std; typedef long long ll; ll st[1000005], f; ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); } ll phi(ll x) { ll ans = 1, i, y = x; for (i = 2; i * i <= y; ++i) if (x % i == 0) { x /= i, ans *= i - 1; while (x % i == 0) x /= i, ans *= i; } if (x > 1) ans *= x - 1; return ans; } ll quick_mul(ll a, ll b) { ll ans = 0; for (; b; b >>= 1, a = (a << 1) % f) if (b & 1) ans = (ans + a) % f; return ans; } ll quick_pow(ll a, ll b) { ll ans = 1; for (; b; b >>= 1, a = quick_mul(a, a)) if (b & 1) ans = quick_mul(ans, a); return ans; } int main() { int kase = 0; ll x, m, i, sz; while (scanf("%lld", &x) == 1 && x) { f = 9 * x / gcd(8, 9 * x); m = phi(f); for (sz = 0, i = 1; i * i <= m; ++i) if (m % i == 0) { st[sz++] = i; if (i * i != m) st[sz++] = m / i; } sort(st, st + sz); for (i = 0; i < sz; ++i) if (quick_pow(10, st[i]) % f == 1) { printf("Case %d: %lld\n", ++kase, st[i]); i = 2147483640; } if (i != 2147483641) printf("Case %d: 0\n", ++kase); } return 0; }
The Luckiest number
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 4884 Accepted: 1313
Description
Chinese people think of ‘8’ as the lucky digit. Bob also likes digit ‘8’. Moreover, Bob has his own lucky number L. Now he wants to construct his luckiest number which is the minimum among all positive integers that are a multiple of L and consist of only digit ‘8’.Input
The input consists of multiple test cases. Each test case contains exactly one line containing L(1 ≤ L ≤ 2,000,000,000).The last test case is followed by a line containing a zero.
Output
For each test case, print a line containing the test case number( beginning with 1) followed by a integer which is the length of Bob’s luckiest number. If Bob can’t construct his luckiest number, print a zero.Sample Input
811
16
0
Sample Output
Case 1: 1Case 2: 2
Case 3: 0
相关文章推荐
- QT学习笔记(2)—信号与槽
- gradle使用学习(一)————环境与配置
- 代码规范、代码复审、PSP
- C/C++指针函数和函数指针
- 求1加到n的发散思维方法
- Go语言学习(二)常量
- android:layout_gravity和android:gravity的区别
- 非均衡数据学习
- c/c++ socket发送http请求访问网站
- mutable关键字
- 代码规范、代码复审、PSP
- iOS--轮播图实现
- 【技术文】专业对拍20年之批处理(.bat)
- 一篇文章通透理解序列号实现原理
- 程序员必须知道的10大基础实用算法及其讲解
- kettle-step插件结构
- hibernate级联删除
- (在线文库系统)centos nginx mysql php openoffice pdf2swf pdf2image
- 已知最大公约和最小公倍数,求可能的解的组数
- xcode常用的快捷键