#16060: 甚麼情況?!


hshua (hshua)

學校 : 新北市立林口高級中學
編號 : 52506
來源 : [163.20.185.250]
最後登入時間 :
2024-03-15 09:17:14
d271. 11582 - Colossal Fibonacci Numbers! -- UVa11582 | From: [125.227.237.177] | 發表日期 : 2018-11-16 10:03

甚麼情況?!

#0: 100% WA (line:608)

您的答案為: 0
正確答案為: 1

#include <iostream>
using namespace std;
#define ull unsigned long long
ull a,b,sum;
int f[1000];
//==============================================
int main() {
int m, n;

cin>>m;
while(m--){
cin>>a>>b>>n;
f[0]=0; f[1]=1; f[2]=1;

for(int i=3; i<1000; i++){
f[i]=(f[i-1]+f[i-2])%n;
//printf("@@@ f[%d]=%lld\n",i,f[i]);
}

sum=1;
while(b>0){ //快速冪
if(b%2) sum*=(a%n);
a = (a%n)*(a%n);
a %= n;
sum %= n;
b/=2;
//printf("%lld %lld %lld\n",a,b,sum);
}
//cout<<sum<<endl;
cout<<f[sum]<<endl;
}
}

請指教
 
#16065: Re:甚麼情況?!


OwO310659 (OwO)

學校 : 新北市立板橋高級中學
編號 : 58647
來源 : [118.150.111.60]
最後登入時間 :
2024-04-25 01:16:40
d271. 11582 - Colossal Fibonacci Numbers! -- UVa11582 | From: [106.105.27.148] | 發表日期 : 2018-11-16 17:46

你的方法是假設 f[i]%n 會每 n 個循環一次,
但事實上 f[i]%n 並不會每 n 個循環一次,
以下簡單舉例:

f[i]    : 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...
f[i]%2 : 0, 1, 1, 0, 1, 1, 0,   1,   1,   0,  1, ...  (每3個循環一次)
f[i]%3 : 0, 1, 1, 2, 0, 2, 2,   1,   0,   1,  1, ...  (每8個循環一次)
 
 
 以上希望有幫助到你~ OwO

 
ZeroJudge Forum