HDU 5212 Code

来源:互联网 发布:ezdmc编程 编辑:程序博客网 时间:2024/05/29 08:16
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
51 3 4 2 4
 

Sample Output
64
Hint

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

自己yy的做法,明明是可以过的,比赛的时候用了longlong转换int结果一直wa,也是醉了。

#include<stdio.h>#include<string.h>#include<math.h>#include<algorithm>#include<stack>using namespace std;const int maxn = 10005;const int base = 10007;int T, n, k, a[maxn], f[maxn], x, y, tot, F[maxn], ans;bool cmp(const int &a, const int &b){return a > b;}struct abc{int x, y;abc(int x, int y) :x(x), y(y){};abc(){};bool operator<(const abc&a){return x > a.x;}}b[maxn];int main(){while (scanf("%d", &n) != EOF){memset(f, 0, sizeof(f));memset(F, 0, sizeof(F));ans = 0;for (int i = 1; i <= n; i++){scanf("%d", &a[i]);ans = (ans + a[i] * (a[i] - 1)) % base;}sort(a + 1, a + n + 1, cmp);for (int i = 1; i <= n; i++){tot = 0;int k = sqrt(1.0*a[i]);for (int j = 1; j <= k; j++){if (a[i] % j == 0){b[tot++] = abc(j, f[j]++);if (a[i] != j*j) b[tot++] = abc(a[i] / j, f[a[i] / j]++);}}sort(b, b + tot);for (int j = 0; j < tot; j++)for (int k = j + 1; k < tot; k++)if (b[j].x%b[k].x == 0) b[k].y -= b[j].y;for (int j = 0; j < tot; j++) F[b[j].x] += b[j].y;}for (int i = 2; i < maxn; i++){x = 2 * F[i] % base;x = x*i % base;x = x*(i - 1) % base;ans = (ans + x) % base;ans = (ans + base) % base;}printf("%d\n", ans);}return 0;}


0 0