Code HDU - 5212 容斥

本文提供了一道来自HDU在线评测系统的题目解析,该题要求实现一个计算特定数学表达式的函数。文章详细展示了通过记录每个整数因子出现次数,并利用这些信息来高效计算所有可能的两两整数对的最大公约数及其相关表达式的值。

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






### 关于HDU - 6609 的题目解析 由于当前未提供具体关于 HDU - 6609 题目的详细描述,以下是基于一般算法竞赛题型可能涉及的内进行推测和解答。 #### 可能的题目背景 假设该题目属于动态规划类问题(类似于多重背包问题),其核心在于优化资源分配或路径选择。此类问题通常会给出一组物品及其属性(如重量、价值等)以及约束条件(如量限制)。目标是最优地选取某些物品使得满足特定的目标函数[^2]。 #### 动态转移方程设计 如果此题确实是一个变种的背包问题,则可以采用如下状态定义方法: 设 `dp[i][j]` 表示前 i 种物品,在某种条件下达到 j 值时的最大收益或者最小代价。对于每一种新加入考虑范围内的物体 k ,更新规则可能是这样的形式: ```python for i in range(n): for s in range(V, w[k]-1, -1): dp[s] = max(dp[s], dp[s-w[k]] + v[k]) ``` 这里需要注意边界情况处理以及初始化设置合理值来保证计算准确性。 另外还有一种可能性就是它涉及到组合数学方面知识或者是图论最短路等相关知识点。如果是后者的话那么就需要构建相应的邻接表表示图形结构并通过Dijkstra/Bellman-Ford/Floyd-Warshall等经典算法求解两点间距离等问题了[^4]。 最后按照输出格式要求打印结果字符串"Case #X: Y"[^3]。 #### 示例代码片段 下面展示了一个简单的伪代码框架用于解决上述提到类型的DP问题: ```python def solve(): t=int(input()) res=[] cas=1 while(t>0): n,k=list(map(int,input().split())) # Initialize your data structures here ans=find_min_unhappiness() # Implement function find_min_unhappiness() res.append(f'Case #{cas}: {round(ans)}') cas+=1 t-=1 print("\n".join(res)) solve() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值