Hdoj 2817 A sequence of numbers (fast power modulo)

Source: Internet
Author: User

A sequence of numbers http://acm.hdu.edu.cn/showproblem.php?pid=2817
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 4046 Accepted Submission (s): 1242


problem DescriptionXINLV wrote some sequences on the paper a long time ago, they might is arithmetic or geometric sequences. The numbers is not very clear now, and only the first three numbers of each sequence is recognizable. XINLV wants to know some numbers in these sequences, and he needs your help. 
InputThe first line contains a integer n, indicting that there is n sequences. Each of the following N lines contain four integers. The first three indicating the first three numbers of the sequence, and the last one are K, indicating that we want to know The k-th numbers of the sequence.

You can assume 0 < K <= 10^9, and the other three numbers is in the range [0, 2^63]. All the numbers of the sequences is integers. And the sequences are non-decreasing.
 
OutputOutput one line for each test case, which is, the k-th number module (%) 200907. 
Sample Input
21 2 3 51 2 4 5
 
Sample Output
516
 
Sourcemulti-university Training Contest 1-host by Tju 
recommendGaojie | We have carefully selected several similar problems for you:2818 2819 2825 2824 2822

#include <cstdio>

#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace Std;
const int mod=200907;
__int64 fun1 (__int64 q,__int64 N)//a1*q^ (n-1)//geometric series
{
__int64 ans=1,base=q;
while (n)
{
if (n&1)
Ans=ans*base%mod;
Base=base*base%mod;
n>>=1;
}
return ans%mod;
}



__int64 fun2 (__int64 a1,__int64 d,__int64 N)//a1+ (n-1) *d;
{
Return a1%mod+ ((n-1)%mod*d%mod)%mod;
}


int Jude (__int64 num1,__int64 Num2,__int64 num3)
{
if (num1+num3==2*num2)
return 0;
Else
return 1;
}


int main ()
{
int n;
scanf ("%d", &n);
while (n--)
{
__int64 a1,a2,a3,k;
scanf ("%i64d%i64d%i64d%i64d", &a1,&a2,&a3,&k);
if (Jude (A1,A2,A3))
{
__int64 q=a3/a2;
printf ("%i64d\n", (A1%MOD*FUN1 (q,k-1)%mod)%mod);
}
Else
{
__int64 d=a3-a2;
printf ("%i64d\n", Fun2 (a1,d,k)%mod);
}
}
return 0;
}

Hdoj 2817 A sequence of numbers (fast power modulo)

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.