The Luckiest number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1163    Accepted Submission(s): 363

Problem 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
8
11
16
0
 
Sample Output
Case 1: 1
Case 2: 2
Case 3: 0
思路:欧拉函数;
其实这题和hdu3307,基本一样,只不过这个推下。
设f[n],表示n位全是8的数,那么f[n]=10*f[n-1]+8,那么构造等比数列f[n]+(8/9)=10*(f[n-1]+(8/9));
那么f[n] = (8+8/9)*(10)^(n-1)-8/9;f[n] = (8/9)*((10)^n-1);那么就是要求最小的n使f[n]%L=0;
那么(8/9)*(10^n-1)=k*L;
8/gcd(8,L)*(10^n-1)=9*k*L/(gcd(8,L));
化简为8/gcd(8,L)*(10^n)%(9*L/(gcd(8,L)))=8/gcd(8,L);
8/gcd(8,L)与(9*L/(gcd(8,L))互质可以消去,的10^n%(9*L/(gcd(8,L)))=1;
那么用另模数为m,10^n%(m)=1;

m和10必定互质,否则无解。

于是根据欧拉定理,10^(Euler(m)) = 1(mod m) 。由于题目要求最小的解,解必然是Euler(m)的因子。

需要注意的是,对于10^x,由于m太大,直接快速幂相乘的时候会超long long

这题我开始用baby-step,超时了;

  1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<queue>
6 #include<set>
7 #include<math.h>
8 #include<map>
9 using namespace std;
10 typedef long long LL;
11 pair<LL,LL>exgcd(LL n,LL m);
12 LL gcd(LL n,LL m);
13 LL quick(LL n,LL m,LL mod);
14 LL mul(LL n, LL m,LL p);
15 int slove(LL n);
16 LL phi(LL n);
17 bool prime[1000005];
18 int ans[1000005];
19 LL fen[1000005];
20 int main(void)
21 {
22 LL n;
23 int i,j;
24 int cn = 0;
25 for(i = 2; i <= 1000; i++)
26 {
27 if(!prime[i])
28 {
29 for(j = i; (i*j) <= 1000000; j++)
30 {
31 prime[i*j] = true;
32 }
33 }
34 }
35 for(i = 2; i <= 1000000; i++)
36 {
37 if(!prime[i])
38 {
39 ans[cn++] = i;
40 }
41 }
42 //printf("%d\n",cn);
43 int __ca = 0;
44 while(scanf("%lld",&n),n!=0)
45 {
46 LL gc = gcd(8,n);
47 n = 9*n/gc;
48 LL oula = phi(n);
49 LL x = gcd(n,10);//printf("%lld\n",n);
50 //printf("%lld\n",x);
51 printf("Case %d: ",++__ca);
52 if(x!=1)
53 {
54 printf("0\n");
55 }
56 else
57 {
58 int k = slove(oula);
59 //printf("%d\n",k);
60 for(i = 0;i < k;i++)
61 {
62 LL akk =quick(10,fen[i],n);
63 if(akk==1)
64 {
65 break;
66 }
67 }//printf("%d\n",10);
68 printf("%lld\n",fen[i]);
69 }
70 }
71 return 0;
72 }
73 int slove(LL n)
74 { int cn = 0;int i,j;
75 for(i = 1;i < sqrt(1.0*n);i++)
76 {
77 if(n%i==0)
78 {
79 if(n/i==i)
80 {
81 fen[cn++] = i;
82 }
83 else
84 {
85 fen[cn++] = i;
86 fen[cn++] = n/i;
87 }
88 }
89 }
90 sort(fen,fen+cn);
91 return cn;
92 }
93 LL phi(LL n)
94 {
95 int f = 0;
96 bool flag = false;
97 LL ask =n;
98 while(n>1)
99 {
100 while(n%ans[f]==0)
101 {
102 if(!flag)
103 {
104 flag = true;
105 ask/=ans[f];
106 ask*=ans[f]-1;
107 }
108 n/=ans[f];
109 }
110 f++;
111 flag = false;
112 if((LL)ans[f]*(LL)ans[f]>n)
113 {
114 break;
115 }
116 }
117 if(n > 1)
118 {
119 ask/=n;
120 ask*=(n-1);
121 }
122 return ask;
123 }
124 pair<LL,LL>exgcd(LL n,LL m)
125 {
126 if(m==0)
127 return make_pair(1,0);
128 else
129 {
130 pair<LL,LL>ak = exgcd(m,n%m);
131 return make_pair(ak.second,ak.first-(n/m)*ak.second);
132 }
133 }
134 LL gcd(LL n,LL m)
135 {
136 if(m==0)
137 return n;
138 else return gcd(m,n%m);
139 }
140 LL quick(LL n,LL m,LL mod)
141 {
142 LL ak = 1;
143 n %= mod;
144 while(m)
145 {
146 if(m&1)
147 ak =mul(ak,n,mod);
148 n = mul(n,n,mod);
149 m>>=1;
150 }
151 return ak;
152 }
153 LL mul(LL n, LL m,LL p)
154 {
155 n%=p;
156 m%=p;
157 LL ret=0;
158 while(m)
159 {
160 if(m&1)
161 {
162 ret=ret+n;
163 ret%=p;
164 }
165 m>>=1;
166 n<<=1;
167 n%=p;
168 }
169 return ret;
170 }

The Luckiest number(hdu2462)的更多相关文章

  1. 4.Single Number && Single Number (II)

    Single Number: 1. Given an array of integers, every element appears twice except for one. Find that ...

  2. PAT 甲级 1019 General Palindromic Number(20)(测试点分析)

    1019 General Palindromic Number(20 分) A number that will be the same when it is written forwards or ...

  3. Python3 数字Number(六)

    Python 数字数据类型用于存储数值. 数据类型是不允许改变的,这就意味着如果改变数字数据类型得值,将重新分配内存空间. 以下实例在变量赋值时 Number 对象将被创建: var1 = 1 var ...

  4. BZOJ 3000: Big Number (数学)

    题目: https://www.lydsy.com/JudgeOnline/problem.php?id=3000 题解: 首先n很大,O(n)跑不过,那么就要用一些高端 而且没听过 的东西——sti ...

  5. 【CF1017C】The Phone Number(构造)

    题意:要求构造一个1-n的排列,使得它的LIS+LDS最小 n<=1e5 思路:一个百度之星时候从LYY处听来的结论:1-n随机排列的LIS期望是根号级别的 考虑将LIS与LDS都构造成根号级别 ...

  6. Python学习笔记 (2.1)标准数据类型之Number(数字)

    Python3中,数字分为四种——int,float,bool,complex int(整型) 和数学上的整数表示没啥区别,没有大小限制(多棒啊,不用写整数高精了),可正可负.还可表示16进制,以 0 ...

  7. The Luckiest number(hdu 2462)

    给定一个数,判断是否存在一个全由8组成的数为这个数的倍数 若存在则输出这个数的长度,否则输出0 /* 个人感觉很神的一道题目. 如果有解的话,会有一个p满足:(10^x-1)/9*8=L*p => ...

  8. POJ 3696 The Luckiest number (欧拉函数,好题)

    该题没思路,参考了网上各种题解.... 注意到凡是那种11111..... 22222..... 33333.....之类的序列都可用这个式子来表示:k*(10^x-1)/9进而简化:8 * (10^ ...

  9. 第一届山东省ACM——Phone Number(java)

    Description We know that if a phone number A is another phone number B’s prefix, B is not able to be ...

随机推荐

  1. HDFS04 HDFS的读写流程

    HDFS的读写流程(面试重点) 目录 HDFS的读写流程(面试重点) HDFS写数据流程 网络拓扑-节点距离计算 机架感知(副本存储节点的选择) HDFS的读数据流程 HDFS写数据流程 客服端把D: ...

  2. Java实现单链表的增删查改及逆置打印

    //所提供的接口 LinkList.java package Struct; public interface LinkList {//判断链表为空public boolean linkListIsE ...

  3. 查看linux系统CPU和内存命令

    cat /proc/cpuinfo查看linux系统的CPU型号.类型以及大小,如下图所示.   通过greap命令根据Physical Processor ID筛选出多核CPU的信息.   cat ...

  4. RAC中常见的高级用法-过滤

    filter      过滤信号,使用它可以获取满足条件的信号. - (void)filter { //只有当我们文本框内容长度大于5才想要获取文本框的内容 [[_passWord.rac_textS ...

  5. redis入门到精通系列(六):redis的事务详解

    (一)事务的概念 谈到数据库的高级应用,不可避免会谈到事务.熟悉mysql的朋友们对事务肯定不陌生,简单来讲事务就是控制一个数据库操作序列要么全部执行要么全部不执行.今天我们就来了解redis中的事务 ...

  6. 【Linux】【Services】【Disks】zfs

    1. 简介: 据说zfs有去除重复数据的功能,无良人士继续要求吧samba共享盘使用的centos7上自带的xfs改成zfs,并且开启去重功能.samba配置见 http://www.cnblogs. ...

  7. 阿里云esc 安装 docker

    1. 更新 yum 到最新: yum update (用 root 用户登录,无需加 sudo,如果不是,需要加,即  yum update ) 2. 安装软件包:yum-util(提供 yum-co ...

  8. redis的总结笔记

    # Redis    1. 概念: redis是一款高性能的NOSQL系列的非关系型数据库        1.1.什么是NOSQL            NoSQL(NoSQL = Not Only ...

  9. 开发中Design Review和Code Review

    一.Design Review 详解 翻译为设计评审,也就是对需求设计进行审核,防止出现异常问题,例如下面的这些 可用性 外部依赖有哪些?如果这些外部依赖崩溃了我们有什么处理措施? 我们SLA是什么? ...

  10. 测试工具_siage

    目录 一.简介 二.例子 三.参数 一.简介 Siege是一个多线程http负载测试和基准测试工具. 1.他可以查看每一个链接的状态和发送字节数 2.可以模拟不同用户进行访问 3.可以使用POST方法 ...