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

求救,本想偷懒,现在不断完善,真不知道哪里有问题了,大牛们,帮忙瞄下

Posted by ACM_Hohai at 2008-11-01 21:31:50 on Problem 1282
#include<iostream>

/*
这里用了计算各个祭祀的周期,求最小公倍数
*/

using namespace std;

int d[201][201];
int flag[201][201];
int ds[201];

__int64 gcd(__int64 a,__int64 b)
{
    __int64 r=0;
    while(b!=0) {
        r=a%b;
        a=b;
        b=r;
    }
    return a;
}

__int64 gcds(int *t,int n)
{
    __int64 re=t[0];
    for(int i=1;i<n;i++) {
        re=gcd(re,t[i]);
    }
    return re;
}

__int64 comm_mul(__int64 a,__int64 b)
{
    return a/gcd(a,b)*b;
}

__int64 min_mul(int *t,int n)
{
    __int64 gcdd=gcds(ds,n);//公共最大公约数
    __int64 re=t[0];
    for(int i=1;i<n;i++) {
        re=comm_mul(re,t[i]);
    }
    return re;
}

__int64 solve(int n,int p)
{
    __int64 re=0;
    int x,y;
    bool is_valid=true;
    for(int i=0;i<n&&is_valid;i++) {
        re=0;
        //x=i;y=0;
        x=d[i][0]-1;
        y=1;
        bool f=false;
        for(int t=0;t<p;t++) {
            if(d[i][t]!=i+1)
            {    
                f=true;
                break;
            }
        }
        if(f) {
            memset(flag,0,sizeof(flag));
            while((x!=i||y!=0)&&!flag[x][y]) {
                flag[x][y]=true;
                x=d[x][y]-1;
                y=(++y)%p;
                re++;
            }
            //cout<<re+1<<"\t";
            if(x==i&&y==0) {
                ds[i]=re+1;
            }
            else
            {
                is_valid=false;
            }
        }
        else
        {
            ds[i]=1;
        }
    }
    //cout<<endl;
    if(is_valid) {
        return min_mul(ds,n);
    }
    else
    {
        return 0;
    }
}

int main()
{
    int n,p;
    while(cin>>n>>p)
    {
        memset(d,0,sizeof(d));
        memset(ds,0,sizeof(ds));
        for(int i=0;i<n;i++) {
            for(int j=0;j<p;j++) {
                scanf("%d",&d[i][j]);
            }
        }
        __int64 re=solve(n,p);
        if(re>0) {
            cout<<re<<endl;
        }
        else
        {
            cout<<"No one knows."<<endl;
        }
    }
}

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