Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

WA help!

Posted by kenin at 2006-03-27 20:58:58 on Problem 2775
数据太难设计了
#include <stdio.h>
#include <math.h>

struct node {
  long l,r,num;
  long count;
};

long n,total[10010],ans[10010];
long prime[10010],ccount;
bool ok[10010];
long order[10010],tcount;
long outans;
node tree[10010];

void calc_prime()
{
  long i,j;
  ccount=0;
  for (i=2;i<10010;i++) ok[i]=true;
  for (i=2;i<=10010/2;i++) {
    if (!ok[i]) continue;
    j=i*2;
    while (j<10010) {ok[j]=false; j+=i;}
  }
  for (i=2;i<10010;i++) if (ok[i]) prime[ccount++]=i;
}

void init()
{
  long i;
  for (i=0;i<n;i++) scanf ("%ld",&order[i]);
}

void build_tree()
{
  long i,j;
  tcount=0;
  for (i=0;i<n;i++) {
    if (tcount==0) {
      tree[tcount].num=order[i]; tree[tcount].count=1;
      tree[tcount].l=-1; tree[tcount].r=-1; tcount++;
      continue;
    }
    j=0; tree[tcount].num=order[i]; tree[tcount].l=tree[tcount].r=-1;  tree[tcount].count=1;
    while (1) {
      if (order[i]>tree[j].num) {
        tree[j].count++; 
        if (tree[j].r==-1) {tree[j].r=tcount; break;}
        j=tree[j].r;
      }
      if (order[i]<=tree[j].num) {
        tree[j].count++; 
        if (tree[j].l==-1) {tree[j].l=tcount; break;}
        j=tree[j].l;
      }
    }
    tcount++;
  }     
}

void calc_total()
{
  long queue[10010],closed,opened;
  long l,r,i;
  for (i=0;i<=n;i++) total[i]=0;
  queue[0]=0; closed=0; opened=1;
  while (closed<opened) {
    i=queue[closed]; l=tree[i].l; r=tree[i].r;
    if (l!=-1) {total[tree[l].count]--; queue[opened++]=l;}
    if (r!=-1) {total[tree[r].count]--; queue[opened++]=r;}
    total[tree[i].count-1]++;
    closed++;
  }
}

void calc_ans()
{
  long i,j,ttotal=0,temp,k,tans;
  long twice[50],twcount;
  outans=0;
  for (i=n-1;i>0;i--) total[i]+=total[i+1];
  for (i=0;i<=n;i++) ans[i]=0;
  for (i=2;i<=n;i++) {
    temp=i;
    k=(long)sqrt(double(temp));
    for (j=0;j<ccount&&prime[j]<=k;j++) 
      if (temp%prime[j]==0) {
        while (temp%prime[j]==0) {ans[prime[j]]+=total[i]; temp/=prime[j];}
        k=(long)sqrt(double(temp));
      }
    if (temp>1) ans[temp]+=total[i];
  }
  outans=1;
  for (i=0;i<ccount;i++) {
    temp=prime[i]; j=ans[temp]; tans=1; twcount=0;
    while (j>0) {
      twice[twcount++]=j&1; j/=2;
    }    
    for (j=twcount-1;j>-1;j--) {
      tans=tans*tans;
      if (twice[j]==1) tans*=prime[i];
      tans=tans%9901;
    }
    outans*=tans;
    outans=outans%9901;
  }
}

void work()
{
  build_tree();
  calc_total();
  calc_ans();
}

void output()
{
  printf ("%ld\n",outans);
}

int main()
{
  calc_prime();
  while (1)
  {
    scanf ("%ld",&n);
    if (n==0) break;
    init();
    work();
    output();
  }
  return 0;    
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator