hdu 5212(容斥原理),hdu5212原理

來源:互聯網
上載者:User

hdu 5212(容斥原理),hdu5212原理

CodeTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 77    Accepted Submission(s): 27


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 ($) 
Recommendhujie   |   We have carefully selected several similar problems for you:  5213 5209 5208 5205 5204 


給你一個a數組,讓你計算那段代碼的結果,用容斥原理就ok,莫比烏斯反演也行,不過不會莫比烏斯反演(也是容斥原理)orz。。。改天學習下。

代碼如下:

#include<iostream>#include<algorithm>#include<cstdio>#include<vector>#include<cstring>#include<map>using namespace std;typedef long long ll;const int maxn = 1e4 + 10;const ll mod = 10007;#define rep(i,a,b) for(int i=(a);i<(b);i++)#define pb push_backint cnt[maxn],a[maxn];ll d[maxn];int main(){        int n;        while(~scanf("%d",&n)) {            int Mgcd = 1;            for(int i = 0; i < n; i++) {                int x;                scanf("%d",&x);                Mgcd = max(Mgcd,x);                a[i] = x;            }            memset(cnt,0,sizeof(cnt[0])*Mgcd+20);            for(int i = 0; i < n; i++)cnt[a[i]]++;            ll ans = 0,res = 0;            /*d[i]表示任取兩數gcd為i的方案數*/            for(ll i = Mgcd; i > 0; i--) {                ll tot = 0;                for(ll j = i; j <= Mgcd; j+= i)                {                    tot += cnt[j];                    d[i] = (d[i]-d[j])%mod;/*減去gcd為i的倍數的方案數(容斥原理)*/                }                /*gcd為i的方法數等於任選兩個數(可重)是i的倍數的方案                除去gcd為i的倍數的情況(前面已經減掉了)*/                d[i] = (d[i] + tot*tot)%mod;                ans = (d[i]*i*i)%mod;                res = (res+i*d[i])%mod;            }            cout<<((ans-res)%mod+mod)%mod<<endl;        }        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.