hdu 2815 Mod Tree (exBSGS)
//解 K^D ≡ N mod P #include<map> #include<cmath> #include<cstdio> #include<iostream> using namespace std; map<int,int>mp; typedef long long LL; void read(int &x) { x=0; char c=getchar(); while(!isdigit(c)) c=getchar(); while(isdigit(c)) { x=x*10+c-'0'; c=getchar(); } } int Pow(int a,int b,int mod) { int res=1; for(;b;a=1LL*a*a%mod,b>>=1) if(b&1) res=1LL*res*a%mod; return res; } int gcd(int a,int b) { return !b ? a : gcd(b,a%b); } LL exBSGS(int A,int B,int C) { if(B==1) return 0;int k=0,tmp=1,d; while(1) { d=gcd(A,C); if(d==1) break; if(B%d) return -1; B/=d; C/=d; tmp=1LL*tmp*(A/d)%C; k++; if(tmp==B) return k; } mp.clear(); int m=ceil(sqrt(1.0*C)); mp[B]=0; int mul=B; for(int j=1;j<=m;++j) { mul=1LL*mul*A%C; mp[mul]=j; } int am=Pow(A,m,C); mul=tmp; for(int j=1;j<=m;++j) { mul=1LL*mul*am%C; if(mp.find(mul)!=mp.end()) return 1LL*j*m-mp[mul]+k; } return -1; } int main() { int k,p,n; LL d; while(scanf("%d",&k)!=EOF) { read(p); read(n); if(n>=p) { puts("Orz,I can’t find D!"); continue; } d=exBSGS(k,n,p); if(d==-1) puts("Orz,I can’t find D!"); else cout<<d<<'\n'; } }
相關推薦
hdu 2815 Mod Tree (exBSGS)
//解 K^D ≡ N mod P #include<map> #include<cmath> #include<cstdio> #include<iostream> using namespace std; map<int,in
離散對數二連 poj 2417 Discrete Logging & HDU 2815 Mod Tree
tdi close efi 信息 euc xtend spl insert key 題目就不貼了。都是一樣的套路求解a^x % c = b的最小x 註意HDU的題目很坑,有b比c大和題目中輸出信息為全角引號的坑 下面貼的是HDU的版本,poj的改一下a,b,c順序和輸出就好
2815 Mod Tree 【高次不定方程 + 擴充套件 BSGS + 板子】
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7143 Accepted Submis
hdu 3333 Turing Tree(線段樹)
target vector nac ++ uil sim con wrap pro 題目鏈接:hdu 3333 Turing Tree 題目大意:給定一個長度為N的序列。有M次查詢,每次查詢l。r之間元素的總和,同樣元素僅僅算一次。 解題思路:漲姿勢了,線段樹的一
【樹狀數組+離線查詢】HDU 3333 Turing Tree
scan 題意 can size esp ret close 位置 ons https://www.bnuoj.com/v3/contest_show.php?cid=9149#problem/H 【題意】 給定一個數組,查詢任意區間內不同數字之和。 (n<=3000
hdu 1710 Binary Tree Traversals 前序遍歷和中序推後序
rtai clu contains root ron als div 歷遍 case 題鏈;http://acm.hdu.edu.cn/showproblem.php?pid=1710 Binary Tree Traversals Time Limit:
HDU 6035 Colorful Tree(dfs)
() 所有路徑 div long code printf tac type %d 題意:一棵有n個點的樹,樹上每個點都有顏色c[i],定義每條路徑的值為這條路徑上經過的不同顏色數量和。求所有路徑的值的和。 可以把問題轉化為對每種顏色有多少條不同的路徑至少經過這種顏色的點
HDU 3333 Turing Tree (主席樹)
stdin pac clear return sta sin turn -- freopen 題意:給定上一個序列,然後有一些詢問,求區間 l - r 中有多少個不同的數的和。 析:和求區間不同數的方法是一樣,只要用主席樹維護就好。 代碼如下: #pragma comme
2015多校第7場 HDU 5379 Mahjong tree 構造,DFS
print () name long scanf 2015多校 printf color tree 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=5379 題意:一顆n個節點n-1條邊的樹,現在要給每個節點標號(1~n),要求
HDU 5573 Binary Tree (構造)
mat tom 方式 title span cat 但是 them special Binary Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
HDU-5534-Partial Tree
fir fun cst ted () gree where put 由於 Partial Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)To
hdu-3333 Turing Tree 離線區間+樹狀數組(區間不同數的和)
owb lld struct sync link urn pan cout space 題目鏈接: http://acm.hdu.edu.cn/showproblem.php?pid=3333 題目大意: 給出一數組,以及m個查詢區間,每次查詢該區間不同數字的和。相同數字只
HDU - 5534 Partial Tree(每種都裝的完全背包)
words first dir with org sta eas -s ++ Partial Tree In mathematics, and more specifically in graph theory, a tree is an undirected gr
HDU 3333 Turing Tree
get back 維護 n) () truct queue dig print Turing Tree http://acm.hdu.edu.cn/showproblem.php?pid=3333 分析: 這道題目叫 圖靈樹。 離線+樹狀數組。 維護到
HDU 2193 AVL Tree
example 如果 align ans rop 最大 只需要 follow following AVL Tree An AVL tree is a kind of balanced binary search tree. Named after their inv
hdu 5534 Partial Tree(完全揹包)
In mathematics, and more specifically in graph theory, a tree is an undirected graph in which any two nodes are connected by exactly one path. In othe
HDU 1710 Binary Tree Traversals
Binary Tree Traversals A binary tree is a finite set of vertices that is either empty or consists of a root r and two disjoint binary trees ca
HDU 4347-KD-Tree(K臨近)
k-dimension tree,能夠儲存k維資料的一棵樹。這裡這個樹其實是指BST。樸素的BST,像Treap、Splay這些,節點都是儲存一維的資訊,但是KD樹就是它們的拓展,可以儲存多維資料。一維的時候很
HDU 1710 Binary Tree Traversals 題解
由題意得: 1.輸入二叉樹的前序遍歷和中序遍歷,輸出這個二叉樹的後序遍歷 2.前序遍歷的順序是:根-左子樹-右子樹 中序遍歷的順序是:左子樹-根-右子樹 後序遍歷的順序是:左子樹-右子樹-根 3.根據前後遍歷找到根後(由定義得:前序遍歷的第一個為
hdu 5534 Partial Tree【完全揹包】
題意:給n-1個點,每個點有個權值和這個點連線的邊的數量(從1到n-1)現在從裡面選取n個點(可以多次選取)構成一顆樹,求出最大權值。 例如: 4 5 1 4 第一條邊權值為5,連線的邊為1 第二條邊權值為1,連線的邊為2 第三條邊權值為4,連線的邊為3 權值最大情況為權