ZOJ 3785 11th省赛 J What day is that day?【打表找规律】

题目链接

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5272

思路

就是说今天是星期六,问 11+22+33+44+...nn 天后是星期几。
打个表找规律吧,暴力查找下发现循环节是294。
比赛时没往这方面想,以为是递推关系,然后T了无数遍最后还是没做出来,唉总归是题目做的太少了。

以下是打表+暴力找循环节的代码:

#include <bits/stdc++.h>
using namespace std;

int a[1000000+100],p=0;
int main()
{
    //freopen("out.txt","w",stdout);
    int sum=0;
    for(int i=1 ; i<=10000 ; ++i)
    {
        int fac=1;
        for(int j=0 ; j<i ; ++j)
        {
            fac*=i;
            fac%=7;
        }
        sum+=fac;
        sum%=7;
        int ans[]={6,0,1,2,3,4,5};
        //printf(",%d",ans[sum]);
        a[p++]=ans[sum];
    }
    for(int r=2 ; r<=1000 ; ++r)
    {
        bool flag=1;
        for(int i=0 ; i<r ; ++i)
        {
            if(a[i]!=a[i+r])
            {
                flag=0;
                break;
            }
        }
        if(flag)printf("success!!!!::%d\n",r);
    }
    return 0;
}

AC代码

#include <bits/stdc++.h>
using namespace std;

int a[]={-1,0,4,3,0,3,4,4,5,6,3,5,6,5,5,6,1,6,0,5,6,6,0,4,5,2,6,5,5,6,0,3,5,4,5,5,6,1,3,4,6,5,5,6,3,2,6,2,3,3,4,5,2,4,5,4,4,5,0,5,6,4,5,5,6,3,4,1,5,4,4,5,6,2,4,3,4,4,5,0,2,3,5,4,4,5,2,1,5,1,2,2,3,4,1,3,4,3,3,4,6,4,5,3,4,4,5,2,3,0,4,3,3,4,5,1,3,2,3,3,4,6,1,2,4,3,3,4,1,0,4,0,1,1,2,3,0,2,3,2,2,3,5,3,4,2,3,3,4,1,2,6,3,2,2,3,4,0,2,1,2,2,3,5,0,1,3,2,2,3,0,6,3,6,0,0,1,2,6,1,2,1,1,2,4,2,3,1,2,2,3,0,1,5,2,1,1,2,3,6,1,0,1,1,2,4,6,0,2,1,1,2,6,5,2,5,6,6,0,1,5,0,1,0,0,1,3,1,2,0,1,1,2,6,0,4,1,0,0,1,2,5,0,6,0,0,1,3,5,6,1,0,0,1,5,4,1,4,5,5,6,0,4,6,0,6,6,0,2,0,1,6,0,0,1,5,6,3,0,6,6,0,1,4,6,5,6,6,0,2,4,5,0,6,6};
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        char s[][50]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
        printf("%s\n",s[a[n%294==0?294:n%294]]);
        //printf("%d\n",ans);
    }
    return 0;
}

你可能感兴趣的:(ACM)