日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ZOJ3785 What day is that day? 快速幂+找规律

發(fā)布時間:2024/10/6 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ZOJ3785 What day is that day? 快速幂+找规律 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

點擊打開鏈接

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



Input


There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

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


Output


For each test case, output one string indicating 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天后是星期幾

思路:

同余與模算術(shù),利用快速冪取模的算法,時間復(fù)雜度為O(logn)。

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

對7取模之后的結(jié)果,發(fā)現(xiàn)循環(huán)節(jié)長度為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


#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]區(qū)間每個數(shù)n的(n^n)%7,再求a數(shù)組的前綴和b數(shù)組,

sum表示一個循環(huán)節(jié)所貢獻(xiàn)的天數(shù),即sum=(1^1+2^2+3^3+......+41^41+41^42)%7=6;

對于每一個樣例n,直接計算即可


AC代碼:

#include<bits/stdc++.h> using namespace std; char s[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++)//循環(huán)節(jié)為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;//一個循環(huán)節(jié)貢獻(xiàn)的天數(shù)int num=n/42;//循環(huán)節(jié)的個數(shù)int ans=(sum*num%7+b[n%42]%7)%7;printf("%s\n",s[ans]);}return 0; } 另一種計算sum的方法:
#include<bits/stdc++.h> using namespace std; char s[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++)//循環(huán)節(jié)為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; }

總結(jié)

以上是生活随笔為你收集整理的ZOJ3785 What day is that day? 快速幂+找规律的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。