2014 Super training #4G what day is that day? -- Two methods

Source: Internet
Author: User

Original question: zoj 3785 http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemcode = 3785.

The day is Saturday. After 1 ^ 1 + 2 ^ 2 + 3 ^ 3... + N ^ N, what is the day of the week?

At first, I thought it was a problem with the summation of such sub-statements. I did not go to the formula after half a day. The result is not displayed. Two methods were found later.

Method 1: Find the rule

As can be seen from the table, the results of these numbers have 42 loops, so the first 42 are processed directly, and the previous one is used later.

Code:

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <string>using namespace std;#define N 20007int sum[44];string ss[8] = {"Saturday","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday"};void init(){    int n,i,j;    sum[0] = 0;    for(n=1;n<=44;n++)    {        int flag = n%7;        int ans = 1;        for(j=1;j<=n;j++)            ans = (ans*flag)%7;        sum[n] = ans;    }    for(i=1;i<=44;i++)        sum[i] += sum[i-1];}int main(){    int i,j,t,n,ans;    init();    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        ans = (((n/42)%7*(sum[42]%7))%7 + sum[n%42]%7)%7;        cout<<ss[ans]<<endl;    }    return 0;}
View code

 

Method 2: Matrix Multiplication

To be updated

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.