【题目描述】

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

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

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. #include<cstring>
  5. #include<ctime>
  6. #include<cmath>
  7. #include<algorithm>
  8. using namespace std;
  9. #define MAXN 5000010
  10. #define FILE "read"
  11. typedef long long ll;
  12. ll n,x,cnt,len,pr[MAXN],prime[MAXN],isprime[MAXN];
  13. ll gcd(ll a,ll b) {return !b?a:gcd(b,a%b);}
  14. 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;}
  15. inline ll read()
  16. {
  17. ll x=,f=; char ch=getchar();
  18. while(!isdigit(ch)) {if(ch=='-') f=-; ch=getchar();}
  19. while(isdigit(ch)) {x=x*+ch-''; ch=getchar();}
  20. return x*f;
  21. }
  22. void get()
  23. {
  24. for(ll i=;i<=MAXN-;i++)
  25. {
  26. if(!isprime[i]) prime[++cnt]=i;
  27. for(ll j=;j<=cnt&&prime[j]*i<=MAXN-;j++)
  28. {
  29. isprime[prime[j]*i]=;
  30. if(i%prime[j]==) break;
  31. }
  32. }
  33. }
  34. void pre(ll p)
  35. {
  36. ll temp=p-;
  37. for(ll i=;i<=cnt;i++)
  38. {
  39. if(temp%prime[i]==) pr[++len]=prime[i];
  40. while(temp%prime[i]==) temp/=prime[i];
  41. }
  42. if(temp>) pr[++len]=temp;
  43. }
  44. bool check(ll d,ll p)
  45. {
  46. if(gcd(p,d)!=) return ;
  47. for(ll i=;i<=len;i++) if(fast(d,(p-)/(pr[i]),p)==) return ;
  48. return ;
  49. }
  50. int main()
  51. {
  52. freopen(FILE".in","r",stdin);
  53. freopen(FILE".out","w",stdout);
  54. n=read(); x=read();
  55. get();
  56. if(isprime[n+]) {printf("-1\n"); return ;}
  57. pre(n+);
  58. for(ll i=x-;i>;i--) if(check(i,n+)) {printf("%I64d\n",i); return ;}
  59. printf("-1\n");
  60. return ;
  61. }

【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. psd文件导出为图片教程

    美术给过来PSD文件好多层啊.怎么挨个把需要的图片导出来呢. 1. 选中 要导出的图片的图层 2.ctrl+N 新建个文档  然后把图片拉到 新的里面 然后点图像-裁剪 确定就行了,然后ctrl+s保 ...

  2. springboot: 集成freemark模板引擎

    1.freemark简介(摘自:http://blog.csdn.net/liaomin416100569/article/details/78349072) 在互联网软件内容网站中 一般首页的访问量 ...

  3. [转]使用tcpdump抓取HTTP包

    tcpdump -XvvennSs 0 -i eth0 tcp[20:2]=0x4745 or tcp[20:2]=0x4854 0x4745 为"GET"前两个字母"G ...

  4. Iterations --codility

    lesson 1:Iterations 1. BinaryGap-----[100%] Find longest sequence of zeros in binary representation ...

  5. Qt学习之秒表的实现(StopWatch) (转)

    秒表对于我来说并不陌生,在之前自己学习单片机时,实现过秒表和数字钟:基本思路:开启单片机带的定时器,并设置它没10ms溢出一次,分别用三个变量hour,minute,secong记录秒表的时分秒,然后 ...

  6. bzoj 5120 [2017国家集训队测试]无限之环——网络流

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=5120 旋转的话相当于去掉一个插头.新增一个插头,所以在这两个插头之间连边并带上费用即可. 网 ...

  7. promise的学习心得记录

    这里只讲promise,和async的使用方法,不会讲他们的原理. Promise 是异步编程的一种解决方案,可以用于取代传统的回调函数,该变那些函数层层嵌套调用的尴尬局面. 1)promise 基本 ...

  8. sprinboot+redis

    (1)pom.xml引入jar包,如下: <dependency> <groupId>org.springframework.boot</groupId> < ...

  9. 016:Explain

    一. Explain EXPLAIN 官方文档 1.explain说明 explain是解释SQL语句的执行计划,即显示该SQL语句怎么执行的 使用explain的时候,也可以使用desc 5.6 版 ...

  10. 排除maven jar冲突 maven tomcat插件启动报错 filter转换异常

    最近在搞一个ssm+shiro的整合 用的maven tomcat插件 启动的时候报错,提示 maven org.springframework.web.filter.CharacterEncodin ...