游客 Signup | Login
中文 | En

1656 - A sequence of numbers

Xinlv wrote some sequences on the paper a long time ago, they might be arithmetic or geometric sequences. The numbers are not very clear now, and only the first three numbers of each sequence are recognizable. Xinlv wants to know some numbers in these sequences, and he needs your help.

Input

The first line contains an integer N, indicting that there are 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 is 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 are in the range [0, 2^63). All the numbers of the sequences are integers. And the sequences are non-decreasing.

Output

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

Examples

Input

2
1 3 5 8
2 4 8 7

Output

15
128

Solution C

#include<stdio.h>
#define N 200907
int main()
{
    long long a,b,c,d,tmp;
    int i,j,k,n,m;
    while(scanf("%d",&n)!=EOF)
    {
        for(m=0;m<n;m++)
        {
            scanf("%lld%lld%lld%d",&a,&b,&c,&k);
            if(b-a==c-b)
            {
                tmp=(b-a)%N;
                d=((k-3)%N*tmp+c%N)%N;
            }
            else if(b/a==c/b)
            {
                tmp=(c/b)%N;
                d=1;
                k--;
                while(k!=0)
                {
                    j=k%2;
                    if(j==1)
                    {
                        d*=tmp;
                        d%=N;
                    }
                    tmp=(tmp*tmp)%N;
                    k/=2;
                }
                d=d*(a%N)%N;
            }
            printf("%lld\n",d);
        }
    }
    return 0;
}

Solution C++

#include<stdio.h>
#define MOD 200907

long long modular_exponent(long long a,long long b,int n){ //a^b mod n
    long long ret=1;
    for (;b;b>>=1,a=(long long)(((long long)a)*a%n))
        if (b&1)
            ret=(long long)(((long long)ret)*a%n);
    return ret;
}


int main()
{
    double a,b,c;
    int T;
    int k;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%lf%lf%lf%d",&a,&b,&c,&k);
        if(a+c==2*b)
        {
            long long a1=(long long )a;
            long long d=(long long )(b-a);
            int ans=(a1%MOD+((k-1)%MOD)*(d%MOD))%MOD;
            printf("%d\n",ans);
        }
        else
        {
            long long a1=(long long )a;
            long long t1=(long long )(a1%MOD);
            double q1=(b/a);
            long long q2=(long long)q1;
            long long q=(long long)(q2%MOD);
            long long tmp=modular_exponent(q,k-1,MOD);
            int ans=(t1*tmp)%MOD;
            
            
            printf("%d\n",ans);
        }
    }
    return 0;
}
Time Limit 1 second
Memory Limit 32 MB
Discuss Stats
上一题 下一题