【POJ 3696】 The Luckiest number
【题目链接】
http://poj.org/problem?id=3696
【算法】
设需要x个8
那么,这个数可以表示为 : 8(10^x - 1) / 9, 由题, L | 8(10^x - 1) / 9
令d = gcd(L,8),则 :
L | 8(10^x - 1) / 9
9L | 8 (10^x - 1) -> 9L/d | 10^x-1 -> 10^x(mod (9L/d)) = 1
易证a^x(mod n) = 1的最小正整数解是phi(n)的一个约数
那么,求出欧拉函数phi(9L/d),枚举约数,即可
【代码】
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
typedef long long ll; int i,cnt,TC;
ll factor[];
ll d,l,p; inline ll gcd(ll x,ll y)
{
if (y == ) return x;
else return gcd(y,x%y);
}
inline ll phi(ll n)
{
int i;
ll ret = n;
for (i = ; i <= sqrt(n); i++)
{
if (n % i == )
{
ret = ret / i * (i - );
while (n % i == ) n /= i;
}
}
if (n > ) ret = ret / n * (n - );
return ret;
}
inline ll mul(ll a,ll b,ll p)
{
ll ans = ;
while (b)
{
if (b & ) ans = (ans + a) % p;
a = a * % p;
b >>= ;
}
return ans;
}
inline ll power(ll a,ll n)
{
ll b = a,ret = ;
while (n > )
{
if (n & ) ret = mul(ret,b,d);
b = mul(b,b,d);
n >>= ;
}
return ret;
} int main()
{ while (scanf("%lld",&l) != EOF && l)
{
d = * l / gcd(,l);
printf("Case %d: ",++TC);
if (gcd(,d) != )
{
printf("0\n");
continue;
} else
{
cnt = ;
p = phi(d);
for (i = ; i <= sqrt(p); i++)
{
if (p % i == )
{
factor[++cnt] = i;
if (i * i != p) factor[++cnt] = p / i;
}
}
sort(factor+,factor+cnt+);for (i = ; i <= cnt; i++)
{
if (power(,factor[i]) == )
{
printf("%lld\n",factor[i]);
break;
}
}
}
} return ; }
【POJ 3696】 The Luckiest number的更多相关文章
- 【POJ 3694】 Network(割边<桥>+LCA)
[POJ 3694] Network(割边+LCA) Network Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7971 ...
- 【POJ 3071】 Football(DP)
[POJ 3071] Football(DP) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4350 Accepted ...
- 【POJ 3140】 Contestants Division(树型dp)
id=3140">[POJ 3140] Contestants Division(树型dp) Time Limit: 2000MS Memory Limit: 65536K Tot ...
- 【POJ 2400】 Supervisor, Supervisee(KM求最小权匹配)
[POJ 2400] Supervisor, Supervisee(KM求最小权匹配) Supervisor, Supervisee Time Limit: 1000MS Memory Limit ...
- 【POJ 1716】Integer Intervals(差分约束系统)
id=1716">[POJ 1716]Integer Intervals(差分约束系统) Integer Intervals Time Limit: 1000MS Memory L ...
- 【POJ 1275】 Cashier Employment(差分约束系统的建立和求解)
[POJ 1275] Cashier Employment(差分约束系统的建立和求解) Cashier Employment Time Limit: 1000MS Memory Limit: 10 ...
- 【POJ 2777】 Count Color(线段树区间更新与查询)
[POJ 2777] Count Color(线段树区间更新与查询) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4094 ...
- 【POJ 1201】 Intervals(差分约束系统)
[POJ 1201] Intervals(差分约束系统) 11 1716的升级版 把原本固定的边权改为不固定. Intervals Time Limit: 2000MS Memory Limit: ...
- 【POJ 2486】 Apple Tree(树型dp)
[POJ 2486] Apple Tree(树型dp) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8981 Acce ...
随机推荐
- 3A课程笔记分享_StudyJams_2017
课程3A-面向对象编程(上) 概述 面向对象的思想在当今的软件开发中占据着主导地位. Java是一门完全面向对象的语言,是一种天然的分布式互联网软件的开发语言,在当今企业级应用中占据绝对领先地位,也是 ...
- IOS 监控网络变化案例源码
随着移动网络升级:2G->3G->4G甚至相传正在研发的5G,网络速度是越来越快,但这流量也像流水一般哗哗的溜走. 网上不是流传一个段子:睡觉忘记关流量,第二天房子就归移动了! 这固然是一 ...
- VHDL之concurrent之when
WHEN (simple and selected) It is one of the fundamental concurrent statements (along with operators ...
- NLP:单词嵌入Word Embeddings
深度学习.自然语言处理和表征方法 原文链接:http://blog.jobbole.com/77709/ 一个感知器网络(perceptron network).感知器 (perceptron)是非常 ...
- 阿里巴巴矢量库IconFont__使用小录
使用阿里巴巴矢量库方法虽然不难,但本人记性不好,遂在次记录几笔 阿里巴巴矢量库地址:http://www.iconfont.cn/plus 阿里巴巴矢量库图标好处: 1:图标矢量化 2:自己总结:ic ...
- JSP状态管理_1_Cookie
http协议的无状态性:当浏览器发送请求飞服务器时,服务器相应客户端请求:但当同一个浏览器再次发送请求给浏览器时,服务器并不知道它就是刚才那个客户端. 保存用户状态的两大机制:Session,Cook ...
- js 响应事件
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- Apex语言(九)类的方法
1.方法 方法是对象的行为.如下表: 看书,编程,打球就是方法. 2.创建方法 [格式] 访问修饰符 返回值类型 方法名(形式参数列表){ 方法体; } 访问修饰符:可以为类方法指定访问级别. 例如, ...
- drf02 序列化器详解 Serializer
作用 1. 序列化,序列化器会把模型对象转换成字典,经过response以后变成json字符串2. 反序列化,把客户端发送过来的数据,经过request以后变成字典,序列化器可以把字典转成模型3. 反 ...
- 【转载】Java IO基础总结
Java中使用IO(输入输出)来读取和写入,读写设备上的数据.硬盘文件.内存.键盘......,根据数据的走向可分为输入流和输出流,这个走向是以内存为基准的,即往内存中读数据是输入流,从内存中往外写是 ...