UVA 11582-colossal Fibonacci numbers! (number theory) (power modulo by divide and conquer)

Source: Internet
Author: User

Huge Fibonacci Numbers!

The main idea: Fibonacci series F[n], give you a,b,n, beg f[a^b]%n.

Idea: Number theory problem. F[a^b]%n is a cycle, we can find out this cycle will be simplified into f[(a% cycle ) ^b]% cycle by the use of the division of the power to take the model.

Note with unsigned long long (seemingly a long long twice times), or will overflow, and learned a trick ...

Do not know which bug, has been changed the wrong, has been, and later picked up others and their own code of the same change on the right,,,

#include <iostream>//uva#include<cstdio>using namespace Std;typedef unsigned long long ull;//unsigned long The range of long is a long long of twice times the const int N = 1100*1100;int f[n];int pow_mod (ull a,ull n,ull m)//Division method, power modulo {    if (n==0) return 1;    ull x = Pow_mod (a,n/2,m);    ull ans = (ull) x * x% m;    if (n%2==1)   ans = ans*a%m;    return (int) ans; int main () {int t,n;ull a,b;cin >> t;while (t--)    {cin >> a >> b >> n;f[0] = 0, f[1] = 1%n;int ka se;for (int i = 2; I <= n*n+100; i++)        {f[i] = (f[i-1]+f[i-2])% n;if (f[i]==f[1] && f[i-1]==f[0]) {Kase = i- 1;break;}} int ans = Pow_mod (a%kase, B, (ull) kase); cout << F[ans] << Endl;}    return 0;}


UVA 11582-colossal Fibonacci numbers! (number theory) (power modulo by divide and conquer)

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.