Description has only two Sentences

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1108    Accepted Submission(s): 345

Problem Description
an = X*an-1 + Y and Y mod (X-1) = 0.
Your task is to calculate the smallest positive integer k that ak mod a0 = 0.
 
Input
Each line will contain only three integers X, Y, a0 ( 1 < X < 231, 0 <= Y < 263, 0 < a0 < 231).
 
Output
For each case, output the answer in one line, if there is no such k, output "Impossible!".
 
Sample Input
2 0 9
 
Sample Output
1
 
Author
WhereIsHeroFrom
 思路:baby_step;
先构造等比数列,a[n]=X*a[n-1]+Y;那么(a[n]+k)=X(a[n-1]+k);那么展开求得k=(Y)/(X-1);那么a[n]+k=(a[0]+k)*(x)^n;
然后a[n]=(a[0]+k)*(x)^n-k;那么要求最小的n满足a[n]%a[0]=0;那么就是求k*x^n%(a[0]) = k%a[0],那么这个用扩展baby_step;求下就可以了,复杂度(sqrt(a[0])*log(a[0]));
  1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<queue>
6 #include<set>
7 #include<math.h>
8 #include<map>
9 using namespace std;
10 typedef long long LL;
11 typedef struct node
12 {
13 LL x;
14 LL id;
15 } ss;
16 ss ans[600000];
17 ss bns[600000];
18 pair<LL,LL>exgcd(LL n,LL m);
19 LL gcd(LL n,LL m);
20 LL quick(LL n,LL m,LL mod);
21 bool cmp(node p,node q)
22 {
23 if(p.x==q.x)
24 return p.id<q.id;
25 else return p.x<q.x;
26 }
27 LL er(int n,int m,LL k);
28 int main(void)
29 {
30 LL x,y,a;
31 while(scanf("%lld %lld %lld",&x,&y,&a)!=EOF)
32 {
33 int kp = 0;
34 LL t = y/(x-1);//printf("%lld\n",t);
35 LL tt = t%a;
36 if(t%a==0)
37 {
38 printf("1\n");
39 }
40 else
41 {
42 t%=a;
43 int flag = 0;
44 LL cnt = 0;
45 LL as = 1;
46 while(true)
47 {
48 LL gc = gcd(x,a);
49 if(gc == 1)
50 break;
51 else if(t%gc==0)
52 {
53 cnt++;
54 a/=gc;
55 as*=x/gc;
56 t/=gc;
57 as%=a;
58 }
59 else if(t%gc)
60 {
61 kp = 1;
62 break;
63 }
64 if(t*as%gc==t)
65 {
66 flag = 1;
67 break;
68 }
69 }
70 if(kp)printf("Impossible!\n");
71 else if(flag)
72 printf("%lld\n",cnt+1);
73 else
74 {
75 LL v = sqrt(1.0*a);
76 pair<LL,LL>acc = exgcd(x,a);
77 LL xx = quick(x,v,a);
78 int i;//printf("%lld\n",xx);
79 acc.first = (acc.first%a+a)%a;
80 LL sum = t*acc.first%a;
81 int cn = 0;
82 for(i = 1; i <= v; i++)
83 {
84 ans[cn].x= sum%a;
85 ans[cn].id = i;
86 cn++;
87 sum = sum*acc.first%a;
88 }
89 sort(ans,ans+cn,cmp);
90
91 bns[0]=ans[0];
92 LL cc = ans[0].x;
93 int ac = 1;
94 for(i = 1; i < cn; i++)
95 {
96 if(ans[i].x!=cc)
97 {
98 cc = ans[i].x;
99 bns[ac].x = ans[i].x;
100 bns[ac].id = ans[i].id;
101 ac++;
102 }
103 }
104 LL akk = as*tt%a;
105 LL idd;//printf("%lld\n",bns[2].x);
106 for(i = 0; i <= v; i++)
107 {
108 idd = er(0,ac-1,akk);
109 if(idd!=-1)
110 break;
111 akk = akk*xx%a;
112 }
113 if(i==v+1)
114 {
115 printf("Impossible!\n");
116 }
117 else
118 {
119 printf("%lld\n",cnt+i*v+idd);
120 }
121 }
122 }
123 }
124 return 0;
125 }
126 pair<LL,LL>exgcd(LL n,LL m)
127 {
128 if(m==0)
129 return make_pair(1,0);
130 else
131 {
132 pair<LL,LL>ak = exgcd(m,n%m);
133 return make_pair(ak.second,ak.first-(n/m)*ak.second);
134 }
135 }
136 LL gcd(LL n,LL m)
137 {
138 if(m==0)
139 return n;
140 else return gcd(m,n%m);
141 }
142 LL quick(LL n,LL m,LL mod)
143 {
144 LL ak = 1;
145 n %= mod;
146 while(m)
147 {
148 if(m&1)
149 ak =ak*n%mod;
150 n = n*n%mod;
151 m>>=1;
152 }
153 return ak;
154 }
155 LL er(int n,int m,LL k)
156 {
157 int mid = (n+m)/2;
158 if(n>m)return -1;
159 if(bns[mid].x == k)
160 {
161 return bns[mid].id;
162 }
163 else if(bns[mid].x < k)
164 {
165 return er(mid+1,m,k);
166 }
167 else return er(n,mid-1,k);
168 }

baby_step

Description has only two Sentences(hdu3307)的更多相关文章

  1. hdu 3307 Description has only two Sentences (欧拉函数+快速幂)

    Description has only two SentencesTime Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...

  2. Description has only two Sentences(欧拉定理 +快速幂+分解质因数)

    Description has only two Sentences Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...

  3. HDU 3307 Description has only two Sentences

    数学实在是差到不行了…… #include <cstdio> #include <cstring> #include <algorithm> #include &l ...

  4. hdu3307 欧拉函数

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3307 Description has only two Sentences Time Limit: 3 ...

  5. HDU题解索引

    HDU 1000 A + B Problem  I/O HDU 1001 Sum Problem  数学 HDU 1002 A + B Problem II  高精度加法 HDU 1003 Maxsu ...

  6. [SinGuLaRiTy] 数论题目复习

    [SinGuLaRiTy-1020] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. [CQBZOJ 1464] Hankson 题目描述 H ...

  7. hdu 3307(欧拉函数+好题)

    Description has only two Sentences Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/327 ...

  8. 【LeetCode】884. Uncommon Words from Two Sentences 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计 日期 题目地址:https://leetc ...

  9. 完美解决CodeSmith无法获取MySQL表及列Description说明注释的方案

    问题描述: CodeSmith是现在比较实用的代码生成器,但是我们发现一个问题: 使用CodeSmith编写MySQL模板的时候,会发现一个问题:MySQL数据表中的列说明获取不到,也就是column ...

随机推荐

  1. 3.Median of Two Sorted Arrays Leetcode

    难度系数:5星 /*************************************************************************** 题目:有两个排好序的数组,要求 ...

  2. (转载)java排序实现

    Java实现几种常见排序方法 日常操作中常见的排序方法有:冒泡排序.快速排序.选择排序.插入排序.希尔排序,甚至还有基数排序.鸡尾酒排序.桶排序.鸽巢排序.归并排序等. 冒泡排序是一种简单的排序算法. ...

  3. 详解工作流框架Activiti的服务架构和组件

    摘要:通过这篇文章,可以对工作流有一个基本的认识,为后续工作流框架Activiti的学习打下坚实的基础. 本文分享自华为云社区<BPMN工作流的基本概念!详解工作流框架Activiti的服务架构 ...

  4. Java8 Lambda表达式、函数式接口和方法引用

    目录 Java8 Lambda表达式和函数式接口 Lambda表达式 Lambda的使用 函数式接口FunctionalInterface Java内置四大核心函数式接口 方法引用 构造器引用 Jav ...

  5. Ubuntu Linux安装QT5之旅

    1. QT 版本选择 如何选择QT版本,参考如下介绍 https://www.cnblogs.com/chinasoft/p/15226293.html 2.  在此以5.15.0解说 下载QT 版本 ...

  6. 如何通过 User-Agent 识别百度蜘蛛

    如果有大量的百度蜘蛛抓取网站就需要注意了:有可能是其他爬虫伪造百度蜘蛛恶意抓取网站. 如果遇到这种情况,这时候就需要查看日志来确定是不是真正的百度蜘蛛(baidu spider).搜索引擎蜘蛛.用户访 ...

  7. 集合类——Map集合、Properties属性文件操作

    1.Map集合 Collection集合的特点是每次进行单个对象的保存,若要对一对对象来进行保存就只能用Map集合来保存.即Map集合中一次可以保存两个对象,且这两个对象的关系是key = value ...

  8. 【分布式】ZooKeeper权限控制之ACL(Access Control List)访问控制列表

    zk做为分布式架构中的重要中间件,通常会在上面以节点的方式存储一些关键信息,默认情况下,所有应用都可以读写任何节点,在复杂的应用中,这不太安全,ZK通过ACL机制来解决访问权限问题,详见官网文档:ht ...

  9. spring注解-组件注册

    一.@Configuration+@Bean @Configuration:配置类==配置文件 @Bean:给容器中注册一个Bean:类型为返回值的类型,默认是用方法名作为id @Bean(" ...

  10. LVS nat模型+dr模型

    nat模型 在 rs1 和 rs2  安装httpd  并配置测试页,启动 [root@rs1 ~]# yum install httpd -y[root@rs1 ~]# echo "thi ...