[bzoj]1041: [HAOI2008]圓上的整點

原題鏈接:圓上的整點

看到下面提示裏的視頻就點進去看了。。直接套結論(大霧

提示裏的視頻不完整,完整視頻鏈接:【官方雙語】隱藏在素數規律中的π

引入一個x(kai)函數(周期函數)1,0,-1,0,1,0,-1,0……

總之x(2n)=0,x(4n+1)=1,x(4n+3)=-1。

將r^2分解質因數,然後圓上的整點個數就是

***

簡單來說就是把r分解質因數O(logn),然後對於每一個質因數,把1到p(p爲該質因數的個數)的x值加起來O(logn),最後累乘再成4得到。。代碼實現就很簡單了。。

#include <iostream>
#include <cstdio>
using namespace std;
struct kk{
    int num;int cal;
};
int r,qwq=0,ans=1,tmp;
kk a[1000000];
int ksm(int k,int p);
int kai(int qaq);
int main()
{
    int j=1;
    scanf("%d",&r);
    while(r>1){
        j++;
        if(r%j==0)a[++qwq].num=j;
        while(r%j==0){
            r/=j;
            a[qwq].cal++;
        }
    }
    /*for(int i=1;i<=qwq;i++)
        cout<<a[i].num<<"  "<<a[i].cal<<endl;*/
    for(int i=1;i<=qwq;i++){
        a[i].cal*=2;
        tmp=0;
        for(int po=0;po<=a[i].cal;po++){
            tmp+=kai(ksm(a[i].num,po));
            //cout<<tmp<<' '<<po<<endl;;
        }
        ans*=tmp;
    //cout<<endl;
    }
    ans*=4;
    cout<<ans<<endl;
    return 0;
}
int kai(int qaq){
    if(!(qaq&1))return 0;
    if(!((qaq-1)%4))return 1;
    if(!((qaq-3)%4))return -1;
}
int ksm(int k,int p){
    int ans=1;
    while(p){
        if(p&1)ans*=k;
        k*=k;
        p>>=1;
    }
    return ans;
}

總複雜度大概就是O(logn)(瞎算,逃)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章