【歐拉定理/初等數論】[BZOJ 3884] 上帝與集合的正確用法

來源:互聯網
上載者:User

標籤:bzoj

[Description]求值

[Solution]
不要被無限個2嚇到了,這一題有一些有趣的性質可以發掘的。
這裡介紹兩個解法。

· Solution 1

我們溫習一下歐拉定理:

和它的推廣:

我們發現,這題的n,p並不一定互素啊,怎麼辦呢?我們可以讓他們強行互素。
利用公式:

我們把原題中的p分為2^k+y
所以原式化為

此時y是奇數,和指數互質了!然後就可以愉快地使用歐拉定理–原式化為

我們發現中間的指數一部分又與原問題相似,於是想到可以遞迴求解。
那邊界是什麼呢?我們發現,phi(y)會不斷縮小,而且每次至少會除去一個2,所以遞迴的深度最多為log2(p),當y=1時,返回0即可。

需要事先篩好phi值或者直接需要的時候根號時間計算求解。

複雜度O(p+log2(p))–線性篩/O(log2(p)*sqrt(p))–直接計算。
實踐過程中第二種方法遠遠快於第一種。

· Solution 2

還是根據公式

設答案為f(p),有

同樣遞迴求解即可,複雜度同第一個解。

[Code]

給出兩種解法的代碼,第一種用的線性篩,第二種直接求解。

· Code 1

#include<cstdio>typedef long long ll;const int maxn=10000001;int phi[maxn]={0,1};void MakePhiList(){    for(int i=2;i<maxn;i++) if(!phi[i])    for(int j=i;j<maxn;j+=i){        if(!phi[j]) phi[j]=j;        phi[j]=phi[j]/i*(i-1);    }}ll pow(ll a,int n,int p){    ll ans=1;    while(n) {    if(n&1) ans=ans*a%p;    a=a*a%p; n>>=1;    }    return ans;}ll f(int x){    if(x==1) return 0;    int k=0; while(!(x%2)) x/=2,k++;    return pow(2,(f(phi[x])%phi[x]-k%phi[x]+phi[x])%phi[x]+phi[x],x)<<k;}int main(){    MakePhiList();    int kase; scanf("%d",&kase);    while(kase--){    int x; scanf("%d",&x);    printf("%lld\n",f(x));    }    return 0;}

· Code 2

#include<cmath>#include<cstdio>typedef long long ll;int Phi(int x){    int ans=x;    for(int i=2,lim=sqrt(x)+1;i<lim;i++) if(!(x%i)){        ans-=ans/i;        while(!(x%i)) x/=i;    }    return x>1?ans-ans/x:ans;}ll pow(ll a,ll n,ll p){    ll ans=1;    while(n){    if(n&1) ans=ans*a%p;    a=a*a%p; n>>=1;    }    return ans;}ll f(int x){    if(x==1) return 0;    int phi=Phi(x);    return pow(2,f(phi)+phi,x);}int main(){    int kase; scanf("%d",&kase);    while(kase--){    int x; scanf("%d",&x);    printf("%lld\n",f(x));    }    return 0;}

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

【歐拉定理/初等數論】[BZOJ 3884] 上帝與集合的正確用法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.