Code HDU - 5212 容斥

HDU - 5212 

http://acm.hdu.edu.cn/showproblem.php?pid=5212


WLD 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; 

}
Input
There are Multiple Cases.(At MOST  10 10

For each case: 

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

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

Print an integer,denoting what the function returns.
Sample Input
5
1 3 4 2 4
Sample Output
64


        
  
Hint
gcd(x,y) means the greatest common divisor of x and y.


/**
 *               ii.                                         ;9ABH,
 *              SA391,                                    .r9GG35&G
 *              &#ii13Gh;                               i3X31i;:,rB1
 *              iMs,:,i5895,                         .5G91:,:;:s1:8A
 *               33::::,,;5G5,                     ,58Si,,:::,sHX;iH1
 *                Sr.,:;rs13BBX35hh11511h5Shhh5S3GAXS:.,,::,,1AG3i,GG
 *                .G51S511sr;;iiiishS8G89Shsrrsh59S;.,,,,,..5A85Si,h8
 *               :SB9s:,............................,,,.,,,SASh53h,1G.
 *            .r18S;..,,,,,,,,,,,,,,,,,,,,,,,,,,,,,....,,.1H315199,rX,
 *          ;S89s,..,,,,,,,,,,,,,,,,,,,,,,,....,,.......,,,;r1ShS8,;Xi
 *        i55s:.........,,,,,,,,,,,,,,,,.,,,......,.....,,....r9&5.:X1
 *       59;.....,.     .,,,,,,,,,,,...        .............,..:1;.:&s
 *      s8,..;53S5S3s.   .,,,,,,,.,..      i15S5h1:.........,,,..,,:99
 *      93.:39s:rSGB@A;  ..,,,,.....    .SG3hhh9G&BGi..,,,,,,,,,,,,.,83
 *      G5.G8  9#@@@@@X. .,,,,,,.....  iA9,.S&B###@@Mr...,,,,,,,,..,.;Xh
 *      Gs.X8 S@@@@@@@B:..,,,,,,,,,,. rA1 ,A@@@@@@@@@H:........,,,,,,.iX:
 *     ;9. ,8A#@@@@@@#5,.,,,,,,,,,... 9A. 8@@@@@@@@@@M;    ....,,,,,,,,S8
 *     X3    iS8XAHH8s.,,,,,,,,,,...,..58hH@@@@@@@@@Hs       ...,,,,,,,:Gs
 *    r8,        ,,,...,,,,,,,,,,.....  ,h8XABMMHX3r.          .,,,,,,,.rX:
 *   :9, .    .:,..,:;;;::,.,,,,,..          .,,.               ..,,,,,,.59
 *  .Si      ,:.i8HBMMMMMB&5,....                    .            .,,,,,.sMr
 *  SS       :: h@@@@@@@@@@#; .                     ...  .         ..,,,,iM5
 *  91  .    ;:.,1&@@@@@@MXs.                            .          .,,:,:&S
 *  hS ....  .:;,,,i3MMS1;..,..... .  .     ...                     ..,:,.99
 *  ,8; ..... .,:,..,8Ms:;,,,...                                     .,::.83
 *   s&: ....  .sS553B@@HX3s;,.    .,;13h.                            .:::&1
 *    SXr  .  ...;s3G99XA&X88Shss11155hi.                             ,;:h&,
 *     iH8:  . ..   ,;iiii;,::,,,,,.                                 .;irHA
 *      ,8X5;   .     .......                                       ,;iihS8Gi
 *         1831,                                                 .,;irrrrrs&@
 *           ;5A8r.                                            .:;iiiiirrss1H
 *             :X@H3s.......                                .,:;iii;iiiiirsrh
 *              r#h:;,...,,.. .,,:;;;;;:::,...              .:;;;;;;iiiirrss1
 *             ,M8 ..,....,.....,,::::::,,...         .     .,;;;iiiiiirss11h
 *             8B;.,,,,,,,.,.....          .           ..   .:;;;;iirrsss111h
 *            i@5,:::,,,,,,,,.... .                   . .:::;;;;;irrrss111111
 *            9Bi,:,,,,......                        ..r91;;;;;iirrsss1ss1111
 */
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxx = 1010;
const int mod = 10007;

int gcd(int a,int b)
{
	return !b ? a : gcd(b,a%b); 
}



int a[20000];

int f[20000];
int factor[20000];
int main()
{
	int n;
	while(~scanf("%d",&n))
	{
		memset(factor,0,sizeof(factor));
		int maxn = 0;
		for(int i = 0;i < n;i++)//输入的时候把每个数的因子保存到factor[]; 
		{
			scanf("%d",&a[i]);
			maxn = max(maxn,a[i]);
			for(int j = 1;j*j <= a[i];j++)
			{
				if(!(a[i]%j))
				{
					factor[j]++;
					if((a[i]/j)!=j)
					{
						factor[a[i]/j]++;
					}
				}
			}
		}
		//f[i]为 公因子只是i的数 的对数(aka. cp)("对三,要不起 "的对,不是log)
		// 注意这个 只^ 字  
		//gcd是最大,所以 f[i] = factor[i]*factor[i]后要减去  公因子是i倍数的f[] 
		int ans = 0;
		for(int i = maxn;i >= 1;i--)
		{
			f[i] = factor[i]*factor[i]%mod;
			for(int j = i*2;j <= maxn;j+=i)//因为这个for 用来减 
			{
				f[i] = ((f[i]-f[j]+mod)%mod);
			}
			int now = i*(i-1)%mod;// gcd(a[i],a[j])*(gcd(a[i],a[j])-1)
			ans = ((ans + f[i]*now)%mod);// f[i]*now就是(gcd为i的cp的数量)*(  gcd(a[i],a[j])*(gcd(a[i],a[j])-1)   ) 
			
		}
		printf("%d\n",ans);
		
	 } 





 return 0;
}





  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值