【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 ...
随机推荐
- HTML+CSS(11)
n CSS背景属性 Background-color:背景色. Background-image:背景图片地址.如:background-image:url(images/bg.gif;) Back ...
- Centos7.5 在桌面创建AndroidStudio快捷方式
Centos7 在桌面创建AndroidStudio快捷方式 前言 最近安装了Centos7,打算将开发平台转移到Linux下,安装好AndroidStudio后,桌面没有快捷方式有些不习惯,随自己创 ...
- 苹果双系统win8.1遇到的一些问题
MacBook air是一款不错的电脑,详细没研究就不叙述好与坏了.只此记录自己使用这款笔记本遇到的问题. 一.安装双系统win8.1 1.下载镜像文件—>拷贝到ios内存中,一个8GU盘.ht ...
- ICCV2015上的GazeTracker论文总结
SLAM问题先慢慢编译一段时间,赶紧拾起来GazeTrack的事情...... ICCV2015的大部分paper已经可以下载,文章列表在这个位置. http://www.cvpapers.com/i ...
- 12 Python+selenium对日期控件进行处理(采用执行JS脚本)
[环境信息] Python34+IE+windows2008 [说明] 1.对于日期控件,没有办法通过定位元素再直接传值的方式处理.可以采用执行JavaScript处理. PS:还要去学学js怎么写, ...
- fileupload 上传控件
<div> <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:But ...
- MySQL--增删改查分页存储过程以及事务
添加和修改写在一起了 可以用id判断添加和修改 和事务在一起编码 可以让代码更严谨 在这里简单的说一下事务的四大特性 事务四大特性之原子性:原子性是指事务是一个不可再分割的工作单位,事务中的操作要么都 ...
- 浅谈Json数据格式
我们先来看下w3cschool对json的定义: JSON:JavaScript 对象表示法(JavaScript Object Notation). JSON 是存储和交换文本信息的语法.类似 XM ...
- 面试官问你:MYSQL事务和隔离级别,该如何回答
一.事务 事务是由一组SQL语句组成的逻辑处理单元,是满足 ACID 特性的一组操作,可以通过 Commit 提交一个事务,也可以使用 Rollback 进行回滚.事务具有以下4个属性,通常简称为事务 ...
- 【剑指Offer】45、扑克牌顺子
题目描述: LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到 ...