Acwing-202-最幸运的数字(同余, 欧拉定理)
2024-10-06 21:40:29
链接:
https://www.acwing.com/problem/content/204/
题意:
8是中国的幸运数字,如果一个数字的每一位都由8构成则该数字被称作是幸运数字。
现在给定一个正整数L,请问至少多少个8连在一起组成的正整数(即最小幸运数字)是L的倍数。
思路:
求出式子8(10^x-1)/9为8组成的正整数.
另其为G, 有L|G, 两边同乘9,9L | 8(10^x-1),为了去掉右边的8, 令右边为变为原来的1/8, 左边变为原来的gcd(L, 8)倍.
令d = gcd(8, L), 9L/d 与 8/L互质, 因为9L/d | 8/d(10^x-1), 所以有9L/d | (10^x-1).
剩下就是同余, 10^x = 1 (mod 9L/d)假装是同余符号.
根据欧拉定理, x为fai(9*L/d) 的约数.枚举再暴力判断.
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL n;
LL Pow(LL a, LL b, LL mod)
{
LL res = 1;
while (b)
{
if (b&1)
res = (res*a)%mod;
a = (a*a)%mod;
b >>= 1;
}
return res;
}
LL Euler(LL x)
{
LL res = x;
for (int i = 2;i <= x;i++)
{
if (x%i == 0)
{
while (x % i == 0)
x /= i;
res = res/i*(i - 1);
}
}
if (x > 1)
res = res/x*(x-1);
return res;
}
LL Solve(int cnt, LL mod)
{
LL res = 1e18;
for (LL i = 1;i*i <= cnt;i++)
{
if (cnt%i == 0)
{
// cout << i << endl;
if (Pow(10, i, mod) == 1)
res = min(res, i);
if (Pow(10, cnt/i, mod) == 1)
res = min(res, cnt/i);
}
}
if (res == 1e18)
return 0;
return res;
}
int main()
{
int cnt = 0;
while (~scanf("%lld", &n) && n)
{
LL eul = Euler((9LL*n)/__gcd(8LL, n*9));
// cout << eul << endl;
LL res = Solve(eul, (9LL*n)/__gcd(8LL, n*9));
printf("Case %d: %lld\n", ++cnt, res);
}
return 0;
}
最新文章
- Java多线程系列--“JUC线程池”03之 线程池原理(二)
- Kafka在Centos6.4中的集群搭建
- org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
- bootstrap菜单完美解决---原创
- Flash Air 打包安卓 ane
- Ant学习---第二节:Ant添加文件夹和文件夹集的使用
- VxWorks 6.9 内核编程指导之读书笔记 -- 多任务(二)
- javascript函数的4种调用方式
- motan源码分析六:客户端与服务器的通信层分析
- Windbg调试命令详解(1)
- mysql排序
- docker在CentOS7下部署指南
- 新年Flag,零基础程序媛编程学习计划(持续更新ing)~~
- python第一天,简单输出及基本运算符
- TensorRT caffemodel serialize
- hive orc update
- (转)CentOS7下yum安装mysql配置多实例
- JS截图(html2canvas)
- 《Two Dozen Short Lessons in Haskell》(二十二)递归
- oracle 查看主外键约束
热门文章
- svn服务器端的更新操作
- Postfix to Prefix Conversion &; Prefix to Postfix Conversion
- github与pycharm
- Tomcat: has been normalized to [null] which is not valid
- 项目四:Java秒杀系统方案优化-高性能高并发实战
- MHA原理及搭建
- vue 模拟测试数据构建
- Selenium IDE for firefox
- O035、Nova Suspend / Rescue 操作详解
- webpack抽取CSS文件与CSSTreeShaking