Mod Tree(hdu2815)
Mod Tree
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5934 Accepted Submission(s): 1498

The picture indicates a tree, every node has 2 children.
The depth of the nodes whose color is blue is 3; the depth of the node whose color is pink is 0.
Now
out problem is so easy, give you a tree that every nodes have K
children, you are expected to calculate the minimize depth D so that the
number of nodes whose depth is D equals to N after mod P.
Every cases have only three integers indicating K, P, N. (1<=K, P, N<=10^9)
If you can’t find such D, just output “Orz,I can’t find D!”
1 #include<stdio.h>
2 #include<algorithm>
3 #include<queue>
4 #include<stack>
5 #include<string.h>
6 #include<iostream>
7 #include<math.h>
8 #include<map>
9 using namespace std;
10 typedef long long LL;
11 typedef struct node
12 {
13 LL val;
14 int id;
15 } ss;
16 LL quick(LL n,LL m,LL mod);
17 pair<LL,LL>ex_gcd(LL n,LL m);
18 LL gcd(LL n,LL m);
19 bool cmp(node p,node q);
20 LL g_step_b_step(LL x,LL k,LL z);
21 ss ans[100000];
22 int main(void)
23 {
24 LL x,z,k;
25 while(scanf("%lld %lld %lld",&x,&z,&k)!=EOF)
26 {
27 LL ask = g_step_b_step(x,k,z);
28 if(ask == -1||k >= z)
29 printf("Orz,I can’t find D!\n");
30 else printf("%lld\n",ask);
31 }
32 return 0;
33 }
34 LL g_step_b_step(LL x,LL k,LL z)
35 {
36 LL y = 0;
37 LL xx = 1;
38 while(true)
39 {
40 LL c = xx%z;
41 if(c == k)return y;
42 LL gc = gcd(x,z);
43 if(gc == 1)break;
44 y++;if(k%gc)return -1;
45 z/=gc;k /= gc;
46 xx = xx*(x/gc);
47 xx%=z;
48 }
49 LL zz = sqrt(z) + 1;
50 pair<LL,LL>NI = ex_gcd(x,z);
51 NI.first = (NI.first%z + z)%z;
52 LL NNI = NI.first*(k%z)%z;
53 ans[0].id = 0,ans[0].val = k;
54 for(int i = 1; i <= zz; i++)
55 {
56 ans[i].id = i;
57 ans[i].val = NNI;
58 NNI = NNI*NI.first%z;
59 }
60 sort(ans,ans+zz+1,cmp);
61 LL x1 = quick(x,zz,z);
62 LL slx = xx;
63 for(int i = 0; i <= zz; i++)
64 {
65 int l = 0,r = zz;
66 int id = -1;
67 while(l <= r)
68 {
69 int mid = (l+r)/2;
70 if(ans[mid].val >= slx)
71 {
72 id = mid;
73 r = mid - 1;
74 }
75 else l = mid + 1;
76 }
77 if(id!=-1)
78 {
79 if(ans[id].val == slx)
80 {
81 LL ask = (LL)i*zz + ans[id].id + y;
82 return ask;
83 }
84 }
85 slx = slx*x1%z;
86 }
87 return -1;
88 }
89 LL gcd(LL n,LL m)
90 {
91 if(m == 0)
92 return n;
93 else return gcd(m,n%m);
94 }
95 LL quick(LL n,LL m,LL mod)
96 {
97 n%=mod;
98 LL ask = 1;
99 while(m)
100 {
101 if(m&1)
102 ask = ask*n%mod;
103 n = n*n%mod;
104 m/=2;
105 }
106 return ask;
107 }
108 pair<LL,LL>ex_gcd(LL n,LL m)
109 {
110 if(m == 0)
111 return make_pair(1,0);
112 else
113 {
114 pair<LL,LL>ans = ex_gcd(m,n%m);
115 return make_pair(ans.second,ans.first - (n/m)*ans.second);
116 }
117 }
118 bool cmp(node p,node q)
119 {
120 if(p.val == q.val)
121 return p.id < q.id;
122 else return p.val < q.val;
123 }
Mod Tree(hdu2815)的更多相关文章
- HDU 2815 Mod Tree (扩展 Baby Step Giant Step )
Mod Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- 【HDU2815】【拓展BSGS】Mod Tree
Problem Description The picture indicates a tree, every node has 2 children. The depth of the nod ...
- HDU 2815 Mod Tree 离散对数 扩张Baby Step Giant Step算法
联系:http://acm.hdu.edu.cn/showproblem.php?pid=2815 意甲冠军: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQ ...
- hdu 2815 Mod Tree (exBSGS)
http://acm.hdu.edu.cn/showproblem.php?pid=2815 //解 K^D ≡ N mod P #include<map> #include<cma ...
- hdu 2815 : Mod Tree 【扩展BSGS】
题目链接 直接用模板好了.实在不行,反正有队友啊~~~~ #include<bits/stdc++.h> using namespace std; typedef long long LL ...
- hdu 2815 Mod Tree 高次方程,n不为素数
Accepted 406MS 8576K 2379 B C++/** 这里加了一点限制,,大体还是一样的,, **/ #include <iostream> #include <cs ...
- HDU 2815 Mod Tree
不会,先搁着…… http://blog.csdn.net/acm_cxlove/article/details/7832197
- 2018.9 ECNU ICPC/CCPC Trial Round #2 Query On Tree (树链剖分+线段树维护)
传送门:https://acm.ecnu.edu.cn/contest/105/problem/Q/ 一棵树,支持两种操作:给一条路径上的节点加上一个等差数列;求两点路径上节点和. 很明显,熟练剖分. ...
- hdu6446 Tree and Permutation 2018ccpc网络赛 思维+dfs
题目传送门 题目描述:给出一颗树,每条边都有权值,然后列出一个n的全排列,对于所有的全排列,比如1 2 3 4这样一个排列,要算出1到2的树上距离加2到3的树上距离加3到4的树上距离,这个和就是一个排 ...
随机推荐
- kubernetes部署 docker 容器
docker 容器相对比较简单,不涉及认证授权,只需要本地启动起来即可,唯一需要注意就是添加flannel网络. # yum remove docker-latest-logrotate docker ...
- 【Go语言学习笔记】包
包其实是每个大型工程都会使用的模块化工具. 将相关的代码封装成一个包,给其他项目调用,提供不同的功能. GO的设计是将一个文件夹看成一个包,虽然不一定非要用文件夹的名字,但是比较建议. 同一个文件夹下 ...
- 大数据学习day13------第三阶段----scala01-----函数式编程。scala以及IDEA的安装,变量的定义,条件表达式,for循环(守卫模式,推导式,可变参数以及三种遍历方式),方法定义,数组以及集合(可变和非可变),数组中常用的方法
具体见第三阶段scala-day01中的文档(scala编程基础---基础语法) 1. 函数式编程(https://www.cnblogs.com/wchukai/p/5651185.html): ...
- Kafka 集群安装部署
2.1 安装部署 2.1.1 集群规划 192.168.1.102 192.168.1.103 192.168.1.104 zookeeper zookeeper zookeeper kafka ka ...
- 【Java 泛型】之 <? super T> 和<? extends T> 中 super ,extends如何理解?有何异同?
Java 泛型 <? super T> 和<? extendsT>中 super ,extends怎么 理解?有何不同? 简介 前两篇文章介绍了泛型的基本用法.类型擦除以及泛型 ...
- OC-基础数据类型
七 字符串与基本数据类型转换 获取字符串的每个字符/字符串和其他数据类型转换 八 NSMutableString 基本概念/常用方法 九 NSArray NSArray基本概念/创建方式/注意事项/常 ...
- Javaj基础知识runtime error
遇到的java 运行时错误: NullPointerException空指针 ,简单地说就是调用了未经初始化的对象或者是不存在的对象,这个错误经常出现在创建图片,调用数组这些操作中,比如图片未经初始 ...
- js处理title超长问题
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- SpringBoot(3):SpringData 数据访问
一. 简介 Spring Data是一个用于简化数据库访问,并支持云服务的开源框架:其主要目标是 使得对数据的访问变得方便快捷.对于数据访问层,无论是 SQL(关系型数据库) 还是 NOSQL(非关系 ...
- jQuery中的html()、text()和val()的用法
1.html() 获得的是第一个符合要求的标签中的所有内容,例如: var content = $("li").html(); <li>111<p>999& ...