【题目描述】

Bike是一位机智的少年,非常喜欢数学。他受到142857的启发,发明了一种叫做“循环数”的数。

如你所见,142857是一个神奇的数字,因为它的所有循环排列能由它乘以1,2,...,6(1到它的长度)得到。循环排列意味着将该数的一些数位从尾部挪到前面。例如,12345的循环排列包括:12345,51234,45123,34512,23451.值得一提的是,允许出现前导零。因此4500123和0123450都是0012345的循环排列。你可以看到142857满足条件的原因。以下六个等式是在十进制下的。

·142857*1=142857

·142857*2=285714

·142857*3=428571

·142857*4=571428

·142857*5=714285

·142857*6=857142

现在Bike有一个问题。他将“循环数”扩展到任意进制b。如前所述,142857是十进制下的循环数。另一个例子是二进制下的0011.以下四个等式是二进制的:

·0011*1=0011

·0011*10=0110

·0011*11=1001

·0011*100=1100

他想要找出最大的b(1<b<x)使得有一个b进制下长度为n的正循环数(允许前导零)。

注意,当你将循环数乘以1到其长度的任意整数时你都应该得到一个它的循环排列。

【题解】

传送门:http://blog.csdn.net/wmdcstdio/article/details/45042353?locationNum=7

%%%%%%%%%%%%王梦迪大神

 #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cmath>
#include<algorithm>
using namespace std;
#define MAXN 5000010
#define FILE "read"
typedef long long ll;
ll n,x,cnt,len,pr[MAXN],prime[MAXN],isprime[MAXN];
ll gcd(ll a,ll b) {return !b?a:gcd(b,a%b);}
ll fast(ll a,ll b,ll mod) {ll sum=;for(;b;b>>=,a=a*a%mod)if(b&)sum=sum*a%mod;return sum;}
inline ll read()
{
ll x=,f=; char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') f=-; ch=getchar();}
while(isdigit(ch)) {x=x*+ch-''; ch=getchar();}
return x*f;
}
void get()
{
for(ll i=;i<=MAXN-;i++)
{
if(!isprime[i]) prime[++cnt]=i;
for(ll j=;j<=cnt&&prime[j]*i<=MAXN-;j++)
{
isprime[prime[j]*i]=;
if(i%prime[j]==) break;
}
}
}
void pre(ll p)
{
ll temp=p-;
for(ll i=;i<=cnt;i++)
{
if(temp%prime[i]==) pr[++len]=prime[i];
while(temp%prime[i]==) temp/=prime[i];
}
if(temp>) pr[++len]=temp;
}
bool check(ll d,ll p)
{
if(gcd(p,d)!=) return ;
for(ll i=;i<=len;i++) if(fast(d,(p-)/(pr[i]),p)==) return ;
return ;
}
int main()
{
freopen(FILE".in","r",stdin);
freopen(FILE".out","w",stdout);
n=read(); x=read();
get();
if(isprime[n+]) {printf("-1\n"); return ;}
pre(n+);
for(ll i=x-;i>;i--) if(check(i,n+)) {printf("%I64d\n",i); return ;}
printf("-1\n");
return ;
}

【CF#303D】Rotatable Number的更多相关文章

  1. 【Leet Code】Palindrome Number

    Palindrome Number Total Accepted: 19369 Total Submissions: 66673My Submissions Determine whether an ...

  2. 【CF#338D】GCD Table

    [题目描述] 有一张N,M<=10^12的表格,i行j列的元素是gcd(i,j) 读入一个长度不超过10^4,元素不超过10^12的序列a[1..k],问是否在某一行中出现过 [题解] 要保证g ...

  3. 【codeforces 805D】Minimum number of steps

    [题目链接]:http://codeforces.com/contest/805/problem/D [题意] 给你一个字符串; 里面只包括a和b; 让你把里面的"ab"子串全都去 ...

  4. 【HDU 3709】 Balanced Number (数位DP)

    Balanced Number Problem Description A balanced number is a non-negative integer that can be balanced ...

  5. 【CF 453A】 A. Little Pony and Expected Maximum(期望、快速幂)

    A. Little Pony and Expected Maximum time limit per test 1 second memory limit per test 256 megabytes ...

  6. 【CF 585E】 E. Present for Vitalik the Philatelist

    E. Present for Vitalik the Philatelist time limit per test 5 seconds memory limit per test 256 megab ...

  7. 【35.20%】【CF 706D】Vasiliy's Multiset

    time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standa ...

  8. 【26.8%】【CF 46D】Parking Lot

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  9. 【CF 463F】Escape Through Leaf

    题意 给你一棵 \(n\) 个点的树,每个节点有两个权值 \(a_i,b_i\). 从一个点 \(u\) 可以跳到以其为根的子树内的任意一点 \(v\)(不能跳到 \(u\) 自己),代价是 \(a_ ...

随机推荐

  1. Elixir's keyword lists as option parameters

    备注: 文章转自:https://www.djm.org.uk/posts/writing-extensible-elixir-with-behaviours-adapters-pluggable-b ...

  2. 什么是spark(一) 分区以及和MR的区别

    什么是spark,是一个分布式计算平台,或者说是分布式计算引擎,他的职责就是将指定的数据读入到各个node的内存中,然后计算.所以spark是具有泛化性质的,只要数据源是可读入的,读到内存里面之后,处 ...

  3. SharePoint中使用Visio Service展示业务数据

    SharePoint中可以通过Visio Service可以在浏览器中查看Visio图,功能部署到系统中,一切安好. 而现实总是很折磨人,使用该功能后,相关使用者随后提出,Visio图能否与我的业务数 ...

  4. JAVA Debug 调试代码

    JAVA Debug 调试代码 1.什么时候使用Debug: 程序的运行结果,与你的预期结果不同时,Debug的目的是找错误,而不是该错误: 2.早期调试代码的方式就是打桩: System.out.p ...

  5. (转)Inno Setup入门(十二)——Pascal脚本(1)

    本文转载自:http://blog.csdn.net/yushanddddfenghailin/article/details/17250917 事件函数(1) Inno Setup支持以下函数和过程 ...

  6. Jetty实战(杂七杂八)

    最近开始选择JETTY作为服务器了,乘这现在空闲点学习了些JETTY的部署知识,原来她真的跟TOMCAT很类似,先总结如下: 部署应用方法(下载好jetty); 方法一: 直接将应用的 war包放在j ...

  7. JDK bin指令

    jmap: 一.调用命令示例: jmap -F-dump:format=b,file=rmsheap.bin 6086 就会在当前目录下生成rmsheap.bin的文件,6086是Pid.-F是在某些 ...

  8. Java 版本6下载大全

    Oracle 官方 JDK6 下载地址: 基本包含所有的JDK6版本. 需要登注册相应的账户登录到Oracle官网~ http://www.oracle.com/technetwork/java/ja ...

  9. Hadoop MapReduce任务的启动分析

      正常情况下,我们都是启动Hadoop任务的方式大概就是通过hadoop jar命令(或者写在shell中),事实上运行的hadoop就是一个包装的.sh,下面就是其中的最后一行,表示在其中执行一个 ...

  10. FastJSON使用笔记

    虽然使用FastJSON来生成JSON数据非常简单 最常用的的方法就是JSON.toJSONString()静态方法,这个方法参数可以给一个String,也可以是一个int,也可以给一个Object类 ...