歐拉定理

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   os   io   資料   

http://acm.cug.edu.cn/JudgeOnline/problem.php?cid=1030&pid=0

                      Problem A: 高次同餘Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 43  Solved: 4[Submit][Status][Web Board]Description在數論專題時我給大家講過快速模冪,但是今天的題目只用快速模冪演算法是解決不了的,A^B mod C 中如果B很大怎麼處理呢? 想想我數論專題講的其他內容吧!我的題目就是要你編程求出A^B mod C的值是多少。 Input有多組測試資料,每組測試資料為一行包括三個數A,B,C由一個空格分開。 (1<=A,C<=1000000000,1<=B<=10^10000).Output 對於每組測試資料,輸出一個整數,代表A^B mod C的結果。Sample Input3 2 4 3 4 5 2 10 1000 3 1000000000 7Sample Output1 1 24 4HINT提示:x≥? (n)時,ax≡a(x mod ?(n)+ ?(n)) (mod n)

 知識點:

定義:在數論,對正整數n,歐拉函數是小於等於n的數中與n互質的數的數目。? (n) =   1..n中與n互質的數的個數如何求? (n)?素因子展開+容斥原理令n = p1r1p2r2...pkrk?(n)=n*(1-1/p1)*(1-1/p2)*...*(1-1/pk)(為什嗎?)提示:歐拉函數是積性函數——若m,n互質,即:  φ(mn)=φ(m)φ(n)“被認為是數學世界中最美妙的定理之一”若a和n互質,則a?(n)≡1 (mod n)  (a的?(n)次方)歐拉定理的推廣形式當x≥? (m)時,ax≡a(x mod ?(n)+ ?(n)) (mod n)不需要互素用途:計算高階冪次模數(如何計算?)

 

 

#include<stdio.h> #include<string> #include<iostream> using namespace std; #define eps 1e-8 int Eler(int n) {     int i;     double tmp=(double)n;     for(i=2;i*i<=n;i++)     {         if(n%i==0)         {             tmp*=(1-1.0/i);             while(n%i==0)                 n/=i;         }     }     if(n>1)         tmp*=(1-1.0/n);     tmp+=eps;     return (int)tmp; } int Mod(string b,int q) {     int ret=0;     for(int i=0;i<b.length();i++)     {         ret=ret*10+b[i]-‘0‘;         ret%=q;     }     ret%=q;     return ret; } long long multy(long long q, long long n,int mod) {     long long cnt = n;     long long base = q;     long long ret = 1;     while(cnt > 0)     {         if(cnt & 1)             ret = (long long)ret*base%mod;         cnt = cnt >> 1;         base = (long long)base*base%mod;     }     return ret; } int main() {     int a,c;     string b;     int i;     while(cin>>a>>b>>c)     {         int el=Eler(c);         int tmp=0;           for(i=0;i<b.length();i++)         {             tmp=tmp*10+b[i]-‘0‘;             if(tmp>el)break;         }         if(tmp>el)tmp=Mod(b,el)+el;         //else tmp;         long long ans=multy((long long)a%c,(long long)tmp,c);           printf("%lld\n",ans);         b.clear();     }     return 0; } 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.