hdu 5212 Code

Code

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 456    Accepted Submission(s): 182


Problem Description
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 )

For each case:

The first line contains an integer N(1N10000) .

The next line contains N integers a1,a2,...,aN(1ai10000) .
 

Output
For each case:

Print an integer,denoting what the function returns.
 

Sample Input
  
  
5 1 3 4 2 4 题目大意: 易懂 题目分析: 因为数据范围很小,所以考虑数据范围内的每一个数作为gcd对于整个结果的贡献, 首先对于x,将它作为gcd的两个数一定都是它的倍数,而它的倍数有k个情况下,最多有k^2种情况将它作为最大公约数, 我们设f[x]是x作为最大公约数的数的对数,那么如果是两个是x倍数的数的最大公约数若不是x,那么一定是x的倍数,所以 f[x]+f[2x]+....+f[tx] = k*k , tx <= 10000 , k为x的倍数的个数 那么f[x]=k*k - sigma[2-t](f[tx]), 所以先筛出每个数的倍数的个数,然后利用公式倒序求取之后,再枚举每一个数,求和即可 复杂度n*sqrt(n)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#define MAX 10007
#define MOD 10007

using namespace std;

int f[MAX];
int n,a;
int cnt[MAX];

int main ( )
{
    while ( ~scanf ( "%d" , &n ) )
    {
        memset ( cnt , 0 , sizeof ( cnt ) );
        for ( int i = 1 ; i <= n ; i++ )
        {
            scanf ( "%d" , &a );
            for ( int j = 1 ; j*j <= a ; j++ )
                if ( a%j == 0 )
                {
                    cnt[j]++;
                    if ( a/j != j )
                        cnt[a/j]++;
                }
        }
        int ans = 0;
        for ( int x = 10000 ; x > 0 ; x-- )
        {
            f[x] = cnt[x]*cnt[x]%MOD;
            for ( int i = x*2 ; i <= 10000 ; i += x )
                f[x] = ( f[x] - f[i] + MOD) %MOD;
            int p = x*(x-1)%MOD;
            ans = ( ans + p*f[x]%MOD )%MOD;
        }
        printf ( "%d\n" , ans );
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值