前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >计算日期(快速幂+打表) - ZOJ 3785

计算日期(快速幂+打表) - ZOJ 3785

作者头像
ACM算法日常
发布2018-08-07 17:19:28
4190
发布2018-08-07 17:19:28
举报
文章被收录于专栏:ACM算法日常

新来的紫芝眉宇,参加过亚洲区域赛晋级

It's Saturday today, what day is it after11 + 22 + 33 + ... + NN days?

Input

There are multiple test cases. The firstline of input contains an integer T indicating the number of test cases. Foreach test case:

There is only one line containing oneinteger N (1 <= N <= 1000000000).

Output

For each test case, output one stringindicating the day of week.

Sample Input

2

1

2

Sample Output

Sunday

Thursday

Hint

A week consists of Sunday, Monday, Tuesday,Wednesday, Thursday, Friday and Saturday.

题意:

今天星期六,求1^1+2^2……N^N天后是星期几

思路:

同余与模算术,利用快速幂取模的算法,时间复杂度为O(logn)。

1.先用快速幂求出11 , 22 +,33 , ... ,NN

对7取模之后的结果,发现循环节长度为42,即

(1^1)%7=(43^43)%7,

(2^2)%7=(44^44)%7,

(3^3)%7=(45^45)%7,

(n^n)%7=( (42+n)^(42+n) )%7

源代码:

代码语言:javascript
复制
#include<bits/stdc++.h>
using namespace std;

int fast_pow(int a, int n, int mod)
{
  int ret = 1;

  while (n)
  {
    if (n & 1)
      ret = ret * a % mod;

    a = a * a % mod;
    n >>= 1;
  }

  return ret;
}

int main()
{

  for (int i = 1; i <= 1000; i++)
  {
    printf("%d,", fast_pow(i, i, 7));

    if (i % 7 == 0)
      printf("\n");
  }

  return 0;

}

//以42为一组

1,4,6,4,3,1,0,

1,1,4,2,1,6,0,

1,2,5,1,5,1,0,

1,4,1,4,4,6,0,

1,1,3,2,6,1,0,

1,2,2,1,2,6,0,

1,4,6,4,3,1,0,

1,1,4,2,1,6,0,

1,2,5,1,5,1,0,

1,4,1,4,4,6,0,

1,1,3,2,6,1,0,

1,2,2,1,2,6,0,

1,4,6,4,3,1,0,

1,1,4,2,1,6,0,

1,2,5,1,5,1,0,

1,4,1,4,4,6,0,

1,1,3,2,6,1,0,

1,2,2,1,2,6,0,

1,4,6,4,3,1,0,

1,1,4,2,1,6,0,

1,2,5,1,5,1,0,

1,4,1,4,4,6,0,

1,1,3,2,6,1,0,

1,2,2,1,2,6,0,

1,4,6,4,3,1,0,

1,1,4,2,1,6,0,

1,2,5,1,5,1,0,

1,4,1,4,4,6,0,

1,1,3,2,6,1,0,

1,2,2,1,2,6,0,

1,4,6,4,3,1,0,

1,1,4,2,1,6,0,

1,2,5,1,5,1,0,

1,4,1,4,4,6,0,

1,1,3,2,6,1,0,

1,2,2,1,2,6,0,

1,4,6,4,3,1,0,

1,1,4,2,1,6,0,

1,2,5,1,5,1,0,

1,4,1,4,4,6,0,

1,1,3,2,6,1,0,

1,2,2,1,2,6,0,

1,4,6,4,3,1,0,

1,1,4,2,1,6,0,

1,2,5,1,5,1,0,

1,4,1,4,4,6,0,

1,1,3,2,6,1,0,

1,2,2,1,2,6,0,

2.然后打表求出[1,42]区间每个数n的(n^n)%7,再求a数组的前缀和b数组,

sum表示一个循环节所贡献的天数,即sum=(1^1+2^2+3^3+......+41^41+41^42)%7=6;

对于每一个样例n,直接计算即可

AC代码:C++

代码语言:javascript
复制
#include<bits/stdc++.h>

using namespace std;

chars[10][10] = {"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};

int fast_pow(int a, int n, int mod) //快速幂取模
{
    int ret = 1;

    while (n) {
        if (n & 1)
            ret = ret * a % mod;
        a = a * a % mod;
        n >>= 1;
    }

    return ret;
}

int a[50];//a[n]表示(n^n)%7
int b[50];//b[n]表示a[1]+a[2]+....+a[n]

int main()
{
    ios::sync_with_stdio(0);
    b[0] = 0;

    for (int i = 1; i <= 42; i++) //循环节为42
    {
        a[i] = fast_pow(i, i, 7);
        b[i] = b[i - 1] + a[i];
    }

    int t;

    cin >> t;

    while (t--) {
        int n;
        cin >> n;
        int sum = b[42] % 7; //一个循环节贡献的天数
        int num = n / 42; //循环节的个数
        int ans = (sum * num % 7 + b[n % 42] % 7) % 7;
        printf("%s\n", s[ans]);
    }

    return 0;

}

另一种计算sum的方法:

代码语言:javascript
复制
#include<bits/stdc++.h>
using namespace std;

chars[10][10] = {"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};

int fast_pow(int a, int n, int mod) //快速幂取模
{
    int ret = 1;

    while (n) {
        if (n & 1)
            ret = ret * a % mod;
        a = a * a % mod;
        n >>= 1;
    }

    return ret;
}

int a[50];//a[n]表示(n^n)%7
int b[50];//b[n]表示a[1]+a[2]+....+a[n]

int main()
{
    ios::sync_with_stdio(0);
    b[0] = 0;
    int sum = 0;

    for (int i = 1; i <= 42; i++) //循环节为42
    {
        a[i] = fast_pow(i, i, 7);
        b[i] = b[i - 1] + a[i];
        sum = (sum % 7 + a[i] % 7) % 7; //重点理解
    }

    int t;
    cin >> t;

    while (t--) {
        int n;
        cin >> n;
        int ans = (sum * (n / 42) % 7 + b[n % 42] % 7) % 7;
        printf("%s\n", s[ans]);
    }

    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2018-05-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 ACM算法日常 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档