A sequence of numbers (fast exponentiation)

Source: Internet
Author: User

Title Description

Xinlv 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.

Input requirements

The 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 WAN T 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.

Output requirements

Output one line for each test case, which is, the k-th number module (%) 200907.

If the input
21 2 3 51 2 4 5
should be output
516
The main idea is simple, is to give you a sequence of the first three items, the sequence is not a linear or equal ratio, let you seek the remainder of the K-200907.


The fast exponentiation implementation code is

int Fastpow (int a,int b) {int r=1,base=a;while (b!=0) {if (b&1) r*=base;base*=base;b>>=1;} return r;}

AC Code

#include <iostream> #include <algorithm> #include <vector> #include <string.h> #include < string> #include <cstring> #include <ctype.h> #include <math.h> #include <queue> #include <map>using namespace Std;int mod=200907;long long Fastpow (Long long Q,long long N) {Long long r=1;while (n) {if (n& 1) r=r%mod* (q%mod)%mod;q=q%mod* (Q%mod)%mod;n>>=1;} return r;} void solve (); int main () {solve (); return 0;} void Solve () {   long t,a1,a2,a3,n,d,ans,q;cin>>t;while (t--) {cin>>a1>>a2>>a3> >n;if (A2*2==A1+A3) {d=a2-a1;ans= (a1+ (n-1) *d%mod)%mod;} Else{q=a2/a1;ans=a1*fastpow (q,n-1)%mod;} Cout<<ans<<endl;}}







A sequence of numbers (fast exponentiation)

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.