【题目链接】

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的更多相关文章

  1. 【POJ 3694】 Network(割边&lt;桥&gt;+LCA)

    [POJ 3694] Network(割边+LCA) Network Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7971 ...

  2. 【POJ 3071】 Football(DP)

    [POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted ...

  3. 【POJ 3140】 Contestants Division(树型dp)

    id=3140">[POJ 3140] Contestants Division(树型dp) Time Limit: 2000MS   Memory Limit: 65536K Tot ...

  4. 【POJ 2400】 Supervisor, Supervisee(KM求最小权匹配)

    [POJ 2400] Supervisor, Supervisee(KM求最小权匹配) Supervisor, Supervisee Time Limit: 1000MS   Memory Limit ...

  5. 【POJ 1716】Integer Intervals(差分约束系统)

    id=1716">[POJ 1716]Integer Intervals(差分约束系统) Integer Intervals Time Limit: 1000MS   Memory L ...

  6. 【POJ 1275】 Cashier Employment(差分约束系统的建立和求解)

    [POJ 1275] Cashier Employment(差分约束系统的建立和求解) Cashier Employment Time Limit: 1000MS   Memory Limit: 10 ...

  7. 【POJ 2777】 Count Color(线段树区间更新与查询)

    [POJ 2777] Count Color(线段树区间更新与查询) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4094 ...

  8. 【POJ 1201】 Intervals(差分约束系统)

    [POJ 1201] Intervals(差分约束系统) 11 1716的升级版 把原本固定的边权改为不固定. Intervals Time Limit: 2000MS   Memory Limit: ...

  9. 【POJ 2486】 Apple Tree(树型dp)

    [POJ 2486] Apple Tree(树型dp) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8981   Acce ...

随机推荐

  1. 一周代码秀之[11.18~11.24 linq2xml面向对象]

    1.xml <Sections> <Item key ="1" value ="孕哺期" canBeSelected="false& ...

  2. 精确获取对象的类型:Object.prototype.toString()

    https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/toString

  3. Android开发笔记(12)——ListView & Adapter

    转载请注明:http://www.cnblogs.com/igoslly/p/6947225.html 下一章是关于ListFragment的内容,首先先介绍ListView的相关配置,理解ListF ...

  4. Java_Web之宠物管理系统

    使用JSP+servLet实现宠物管理系统,oraC1e11g作为后台数据厍,实现查看宠物和增加宠物的功能由你实现,如图: 其中宠物包栝:狗.猫.鸟.鼠 具体要求及推荐实现步骤 第一步:创建数据库代码 ...

  5. html 复杂表格

    123456789 123456789 0000000000 日期 123456789 1234560000000789 ----------- ----------- ----------- --- ...

  6. 【Python基础】条件语句

    Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. 可以通过下图来简单了解条件语句的执行过程: Python程序语言指定任何非0和非空(null)值为tr ...

  7. Python数据分析----scipy稀疏矩阵

    一.sparse模块: python中scipy模块中,有一个模块叫sparse模块,就是专门为了解决稀疏矩阵而生.本文的大部分内容,其实就是基于sparse模块而来的 导入模块:from scipy ...

  8. C#第十三节课

    冒泡排序 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System. ...

  9. 06.系统编程-4.多线程和GIL

    为什么有人会说 Python? 多线程是鸡肋?知乎上有人提出这样一个问题,在我们常识中,多进程.多线程都是通过并发的方式充分利用硬件资源提高程序的运行效率,怎么在 Python 中反而成了鸡肋? 有同 ...

  10. Git 基础教程 之 暂存区

    工作区(working directory),就是目录,例如:pythonwork 版本库(Repository),工作区有一个隐藏的目录 .git 就是Git的版本库. 版本库最重要的是称为stag ...