Mod Tree

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

Problem Description

  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.
 
Input
The input consists of several test cases.
Every cases have only three integers indicating K, P, N. (1<=K, P, N<=10^9)
 
Output
The minimize D.
If you can’t find such D, just output “Orz,I can’t find D!”
 
Sample Input
3 78992 453
4 1314520 65536
5 1234 67
 Sample Output
Orz,I can’t find D!
8
20
扩展baby_step,giant_step算法模板题
  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)的更多相关文章

  1. 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 ...

  2. 【HDU2815】【拓展BSGS】Mod Tree

    Problem Description   The picture indicates a tree, every node has 2 children.  The depth of the nod ...

  3. HDU 2815 Mod Tree 离散对数 扩张Baby Step Giant Step算法

    联系:http://acm.hdu.edu.cn/showproblem.php?pid=2815 意甲冠军: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQ ...

  4. hdu 2815 Mod Tree (exBSGS)

    http://acm.hdu.edu.cn/showproblem.php?pid=2815 //解 K^D ≡ N mod P #include<map> #include<cma ...

  5. hdu 2815 : Mod Tree 【扩展BSGS】

    题目链接 直接用模板好了.实在不行,反正有队友啊~~~~ #include<bits/stdc++.h> using namespace std; typedef long long LL ...

  6. hdu 2815 Mod Tree 高次方程,n不为素数

    Accepted 406MS 8576K 2379 B C++/** 这里加了一点限制,,大体还是一样的,, **/ #include <iostream> #include <cs ...

  7. HDU 2815 Mod Tree

    不会,先搁着…… http://blog.csdn.net/acm_cxlove/article/details/7832197

  8. 2018.9 ECNU ICPC/CCPC Trial Round #2 Query On Tree (树链剖分+线段树维护)

    传送门:https://acm.ecnu.edu.cn/contest/105/problem/Q/ 一棵树,支持两种操作:给一条路径上的节点加上一个等差数列;求两点路径上节点和. 很明显,熟练剖分. ...

  9. hdu6446 Tree and Permutation 2018ccpc网络赛 思维+dfs

    题目传送门 题目描述:给出一颗树,每条边都有权值,然后列出一个n的全排列,对于所有的全排列,比如1 2 3 4这样一个排列,要算出1到2的树上距离加2到3的树上距离加3到4的树上距离,这个和就是一个排 ...

随机推荐

  1. UBI 文件系统之分区挂载

    Linux 系统中有关mtd和ubi的接口:(1) cat /proc/mtd:可以看到当前系统的各个mtd情况,(2) cat /proc/partitions: 分区信息,有上面的类似(3) ca ...

  2. 零基础学习java------day11------常用API---Object、Scanner、String、StringBufer/StringBuilder

    API概述 API(application Programming Interface, 应用程序编程接口),是一些预先定义的函数.目的是提供应用程序与开发人员基于某软件或硬件得以访问一组例程的能力, ...

  3. 【leetcode】85. Maximal Rectangle(单调栈)

    Given a rows x cols binary matrix filled with 0's and 1's, find the largest rectangle containing onl ...

  4. HTTP初识

    HTTP(HyperText Transfer Protocol):超文本传输协议. URL(Uniform Resource Locator):统一资源定位符. URI(Uniform Resour ...

  5. oracle中的数组

    Oracle中的数组分为固定数组和可变数组. 一.固定数组固定数组:在定义的时候预定义了数组的大小,在初始化数组时如果超出这个大小,会提示ORA-06532:超出小标超出限制!语法:        T ...

  6. javaIO——输入输出流

    字节流与字符流 File类不支持对文件内容进行相关的操作,所有若要处理文件的内容,则需要通过流操作模式来完成. 流的基本操作步骤: Step1:根据文件路径创建File类对象. Step2:根据字节流 ...

  7. Python 基于python实现的http+json协议接口自动化测试框架源码(实用改进版)

    目录 1.      写在前面 2.      开发环境 3.      大致流程 4.      框架简介 5.      运行结果展示 6.      文件与配置 7.      测试接口实例 n ...

  8. Output of C++ Program | Set 1

    Predict the output of below C++ programs. Question 1 1 // Assume that integers take 4 bytes. 2 #incl ...

  9. Xcode功能快捷键

    隐藏xcode command+h退出xcode command+q关闭窗口 command+w关闭所有窗口 command+option+w关闭当前项目 command+control+w关闭当前文 ...

  10. Dubbo多协议支持

    除了Dubbo服务暴露协议Dubbo协议外,Dubbo框架还支持另外8种服务暴露协议:RMI协议.Hessian协议.HTTP协议.WebService协议.Thrift协议.Memcached协议. ...