【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 ...
随机推荐
- HTTPS的中那些加密算法
密码学在计算机科学中使用非常广泛,HTTPS就是建立在密码学基础之上的一种安全的通信协议.HTTPS早在1994年由网景公司首次提出,而如今在众多互联网厂商的推广之下HTTPS已经被广泛使用在各种大小 ...
- (转)i686只是cpu的指令等级,包括32bit和64bit
i代表intel系列的cpu 386 几乎适用于所有的 x86 平台,不论是旧的 pentum 或者是新的 pentum-IV 与 K7 系列的 CPU等等,都可以正常的工作! 那个 i 指的是 In ...
- js基础标签用法
js是脚本语言,开始标签<script type="text/javascript">.......结束标签</script>.script通常放在< ...
- 解决:惠普HP LaserJet Pro M126a MFP 驱动 安装失败,及其它同类打印机失败问题
注意:如果在 Windows XP 系统下安装出错,请先安装WindowsXP KB971276-v3补丁后再安装装驱动. 下载地址:http://www.dyjqd.com/soft/KB97127 ...
- sql server 查询时间 格式化输出
use test select * from vote insert into vote (contents) values(GETDATE()) insert into vote (contents ...
- 连接(JOIN)运算
内连接--INNER JOIN 此处用商品表(product)和商店商品表(ShopProduct)测试,外键:product_id select sp.shop_id, sp.shop_name, ...
- Django的Error汇总
title: Django学习笔记 catalog: true subtitle: 11. Django_Error汇总 date: 2018-12-14 10:17:28 --- Django的Er ...
- Python爬虫:HTTP协议、Requests库(爬虫学习第一天)
HTTP协议: HTTP(Hypertext Transfer Protocol):即超文本传输协议.URL是通过HTTP协议存取资源的Internet路径,一个URL对应一个数据资源. HTTP协议 ...
- Lua的五种变量类型、局部变量、全局变量、lua运算符、流程控制if语句_学习笔记02
Lua的五种变量类型.局部变量.全局变量 .lua运算符 .流程控制if语句 Lua代码的注释方式: --当行注释 --[[ 多行注释 ]]-- Lua的5种变量类型: 1.null 表示 ...
- 类的定义与实例化、构造函数和初始化表(day04)
十三 类的定义与实例化 类的一般形式 class/struct 类名:继承表{ 访问控制限定符: 类名(形参表):初始化表{}//构造函数 ~类名(void){}//析构函数 返回类型 函数名(形参表 ...