Code( BestCoder Round #39 ($) C) (莫比烏斯反演)

來源:互聯網
上載者:User

標籤:莫比烏斯反演

Code Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 209    Accepted Submission(s): 85



Problem DescriptionWLD likes playing with codes.One day he is writing a function.Howerver,his computer breaks down because the function is too powerful.He is very sad.Can you help him?

The function:


int calc
{
  
  int res=0;
  
  for(int i=1;i<=n;i++)
    
    for(int j=1;j<=n;j++)
    
    {
      
      res+=gcd(a[i],a[j])*(gcd(a[i],a[j])-1);
      
      res%=10007;
    
    }
  
  return res;


InputThere are Multiple Cases.(At MOST 10)

For each case:

The first line contains an integer N(1≤N≤10000).

The next line contains N integers a1,a2,...,aN(1≤ai≤10000).
 
OutputFor each case:

Print an integer,denoting what the function returns. 
Sample Input
51 3 4 2 4
 
Sample Output
64Hintgcd(x,y) means the greatest common divisor of x and y. 
 
SourceBestCoder Round #39 ($)

題意: 簡單易懂,就給你一段代碼,叫你最佳化;

題解: 莫比烏斯反演, 首先我們設f(d)表示在給出的所有數中有f(d)對最大公約數是d. cnt(n) 表示在給出的所有數中有cnt(n)個是n的倍數(包含n). 假設我們已經知道了這兩個函數,然後就可以用莫比烏斯反演做了. 首先F(n) = cnt(n) * cnt(n); 表示大於等於n的所有數組成的對數,那麼有F(n) = f(n) + f(n * 2) + f(n * 3) + .....f(max);
做完以上步驟,就可以套用莫比烏斯反演做了.

莫比無私反演的公式在bin神的部落格上有,去搬吧! 啊啊啊啊啊啊啊啊啊啊啊!!!!!!!!!!!!

AC代碼:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int MAXN = 1e4;typedef long long ll;const int mod = 10007;bool check[MAXN + 10];ll prime[MAXN + 10],cnt[MAXN + 10];ll mu[MAXN + 10],F[MAXN + 10];void Moblus(){    memset(check,false,sizeof(check));    mu[1] = 1;    int tot = 0;    for(int i = 2; i <= MAXN; i++)    {        if(!check[i])        {            prime[tot++] = i;            mu[i] = -1;        }        for(int j = 0; i * prime[j] <= MAXN; j++)        {            check[i * prime[j]] = true;            if(i % prime[j] == 0)            {                mu[i * prime[j]] = 0;                break;            }            mu[i * prime[j]] = -mu[i];        }    }}int main(){    //freopen("in","r",stdin);    Moblus();    int n,x;    while(~scanf("%d",&n))    {        memset(cnt,0,sizeof(cnt));        for(int i = 0; i < n; i++)        {            scanf("%d",&x);            cnt[x]++;        }        for(int i = 1; i <= MAXN; i++)            for(int j = i * 2; j <= MAXN; j += i)                cnt[i] += cnt[j];        for(int i = 1; i <= MAXN; i++) F[i] = cnt[i] * cnt[i];        ll res = 0;        for(int i = 1; i <= MAXN; i++)        {            ll tp = 0;            for(int j = i; j <= MAXN; j += i)            {                tp += mu[j / i] * F[j];                if(tp >= mod) tp %= mod;            }            res += tp * i % mod * (i - 1);            if(res >= mod) res %= mod;        }        printf("%I64d\n",res);    }    return 0;}

 

Code( BestCoder Round #39 ($) C) (莫比烏斯反演)

聯繫我們

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