Discrete Logging

来源:http://www.cnblogs.com/zwfymqz/archive/2017/05/19/6878968.html
-Advertisement-
Play Games

Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5865 Accepted: 2618 Description Given a prime P, 2 <= P < 231, an integer ...


Discrete Logging
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 5865   Accepted: 2618

Description

Given a prime P, 2 <= P < 231, an integer B, 2 <= B < P, and an integer N, 1 <= N < P, compute the discrete logarithm of N, base B, modulo P. That is, find an integer L such that
    B
L
 == N (mod P)

Input

Read several lines of input, each containing P,B,N separated by a space.

Output

For each line print the logarithm on a separate line. If there are several, print the smallest; if there is none, print "no solution".

Sample Input

5 2 1
5 2 2
5 2 3
5 2 4
5 3 1
5 3 2
5 3 3
5 3 4
5 4 1
5 4 2
5 4 3
5 4 4
12345701 2 1111111
1111111121 65537 1111111111

Sample Output

0
1
3
2
0
3
1
2
0
no solution
no solution
1
9584351
462803587

Hint

The solution to this problem requires a well known result in number theory that is probably expected of you for Putnam but not ACM competitions. It is Fermat's theorem that states
   B
(P-1)
 == 1 (mod P)

for any prime P and some other (fairly rare) numbers known as base-B pseudoprimes. A rarer subset of the base-B pseudoprimes, known as Carmichael numbers, are pseudoprimes for every base between 2 and P-1. A corollary to Fermat's theorem is that for any m
   B
(-m)
 == B
(P-1-m)
 (mod P) .

Source

Waterloo Local 2002.01.26 BSGS模板題  
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<map>
 6 #define LL long long 
 7 using namespace std;
 8 LL a,b,c;
 9 map<LL,LL>mp;
10 LL fastpow(LL a,LL p,LL c)
11 {
12     LL base=a;LL ans=1;
13     while(p!=0)
14     {
15         if(p%2==1)ans=(ans*base)%c;
16         base=(base*base)%c;
17         p=p/2;
18     }
19     return ans;
20 }
21 int main()
22 {
23     // a^x = b (mod c)
24     while(scanf("%lld%lld%lld",&c,&a,&b)!=EOF)
25     {
26         LL m=ceil(sqrt(c));// 註意要向上取整 
27         mp.clear();
28         if(a%c==0)
29         {
30         printf("no solution\n");
31         continue;
32         }
33         // 費馬小定理的有解條件 
34         LL ans;//儲存每一次枚舉的結果 b* a^j
35         for(LL j=0;j<=m;j++)  // a^(i*m) = b * a^j
36         {
37             if(j==0)
38             {
39                 ans=b%c;
40                 mp[ans]=j;// 處理 a^0 = 1 
41                 continue;
42             }
43             ans=(ans*a)%c;// a^j 
44             mp[ans]=j;// 儲存每一次枚舉的結果 
45         }
46         LL t=fastpow(a,m,c);
47         ans=1;//a ^(i*m)
48         LL flag=0;
49         for(LL i=1;i<=m;i++)
50         {
51             ans=(ans*t)%c;
52             if(mp[ans])
53             {
54                 LL out=i*m-mp[ans];// x= i*m-j
55                 printf("%lld\n",(out%c+c)%c);
56                 flag=1;
57                 break;
58             }
59             
60         }
61         if(!flag)
62         printf("no solution\n");    
63     }
64     
65     return 0;
66 }

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • Ant path 匹配原則 在Spring MVC中經常要用到攔截器,在配置需要要攔截的路徑時經常用到<mvc:mapping/>子標簽,其有一個path屬性,它就是用來指定需要攔截的路徑的。例如: <mvc:interceptor><mvc:mapping path="/**" /><bean c ...
  • PHP中include和require關鍵字,都可以在一個腳本文件中包含另一個腳本文件,但是兩者卻有幾點不同處: 1.include包含文件,出錯時會產生一個E_WARNING(警告),但是腳本仍舊可以繼續運行 2.require包含文件,會產生一個E_COMPILE_ERROR(錯誤),腳本終止 ...
  • 給出一個長為n的數列,以及n個操作,操作涉及區間加法,單點查值。 這是一道能用許多數據結構優化的經典題,可以用於不同數據結構訓練。 數列分塊就是把數列中每m個元素打包起來,達到優化演算法的目的。 以此題為例,如果我們把每m個元素分為一塊,共有n/m塊,每次區間加的操作會涉及O(n/m)個整塊,以及區間 ...
  • 分塊:顧名思義,把一個區間分成不同的塊,然後由原來的每個點暴力轉換為每個塊的暴力,這樣就大大減小了時間複雜度 可能涉及的幾個詞語解釋: 區間:數列中連續一段的元素 區間操作:將某個區間[a,b]的所有元素進行某種改動的操作 塊:我們將數列劃分成若幹個不相交的區間,每個區間稱為一個塊 整塊:在一個區間 ...
  • a) setter(重要) b) 構造方法(可以忘記),簡單例子: 用的不多,具體的構造函數重構應用可以參考源文檔 c) 介面註入(可以忘記)。 代碼鏈接: http://pan.baidu.com/s/1pKAe5Vt 密碼: qvyy jar 包: 鏈接: http://pan.baidu.co ...
  • R語言數據可視化之ggplot2包,從柱狀圖開始。從簡單的業務量統計開始。 ...
  • ggplot2介紹:內容包含什麼是ggplot2、與lattice包的比較、基本概念、一個例子。 ...
  • 一、基本概念 1.AOP簡介 DI能夠讓相互協作的軟體組件保持鬆散耦合;而面向切麵編程(aspect-oriented programming,AOP)允許你把遍佈應用各處的功能分離出來形成可重用的組件。把這些橫切關註點與業務邏輯相分離正是面向切麵編程(AOP)所要解決的問題 常見場景:日誌、安全、 ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...