hdu 5288OO’s Sequence,hdu5288oosequence

來源:互聯網
上載者:User

hdu 5288OO’s Sequence,hdu5288oosequence

給你n個數,讓你找到所有區間內不能整除其它數的數的個數之和
之前的代碼正確是因為資料比較水

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int mod = 1e9+7;#define ll long long#define N 111111int l[N],r[N];//儲存左邊因子。右邊因子的位置int n,m;int pre[N],last[N];int a[N];vector<int>g[N];int main() {    while(scanf("%d",&n)==1) {        for (int i=101;i<=10000;i++)        g[i].clear();        for(int i=1; i<=n; i++) {            scanf("%d",&a[i]);            if(a[i]>100)            g[a[i]].push_back(i);            l[i]=0;            r[i]=n+1;//初始化最左邊的因子和最右邊的因子都是本身        }        memset(pre,0,sizeof(pre));        memset(last,0,sizeof(last));        int  tmp;        for (int j=1; j<=100; j++) {            tmp=0;            for (int i=1; i<=n; i++) {                if (a[i]%j==0) l[i]=max(l[i],tmp);                if (a[i]==j)                    tmp=i;            }            tmp=n+1;            for (int i=n; i>=1; i--) {                if (a[i]%j==0) r[i]=min(r[i],tmp);                if (a[i]==j)                    tmp=i;            }        }        for (int i=101; i<=10000; i++)            pre[i]=0;//r數組從小到大尋找位置        for (int i=1; i<=n; i++)            if (a[i]>100) {                for (int j=a[i]; j<=10000; j=j+a[i])                    while ((pre[j]<g[j].size())&&(g[j][pre[j]]<i)) //當下標小於i的時候,這個下標所存在數的右因子才可能是i                    {                        r[g[j][pre[j]]]=min(r[g[j][pre[j]]],i);                        if ((pre[j]<g[j].size()-1)&&(g[j][pre[j]+1]<i))//如果成功找到了,那麼繼續向下找過去                            pre[j]++;                        else                            break;                    }            }        for (int i=101; i<=10000; i++)            last[i]=g[i].size()-1;//l數組從大到小尋找位置        for (int i=n; i>=1; i--)            if (a[i]>100) {                for (int j=a[i]; j<=10000; j=j+a[i])                    while ((last[j]>=0)&&(g[j][last[j]]>i)) {                        l[g[j][last[j]]]=max(l[g[j][last[j]]],i);                        if ((last[j]>0)&&(g[j][last[j]-1]>i))                            last[j]--;                        else                            break;                    }            }        /*        for(int i=1;i<=n;i++){            printf("%d %d %d\n",i,l[i],r[i]);        }         */        ll ans = 0;        for(int i=1; i<=n; i++) {            ans = (ans%mod+(ll)(i-l[i])*(r[i]-i)%mod)%mod;        }        printf("%I64d\n",ans);    }}

著作權聲明:都是兄弟,請隨意轉載,請註明兄弟是誰

聯繫我們

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