二分贪心10

来源:互联网 发布:朋友圈推广软件 编辑:程序博客网 时间:2024/06/04 01:13

Description

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.
 

Sample Input

21 2 3 51 2 4 5
 

Sample Output

5

16

给出一个数列的前三个数,不是等差就是等比数列,再求出数列的第K个数,数字大,快速幂取模这个题一直WA,没有通过

  1. #include<stdio.h>  网上一位大佬的
  2. #include<string.h>  
  3. #include<math.h>   
  4. #define M 200907  
  5. int main(){  
  6.     int T;  
  7.     scanf("%d",&T);  
  8.     while(T--){  
  9.         __int64 a,b,c;  
  10.         __int64 k;  
  11.         __int64 m=0;  
  12.         __int64 l=1,r=1;//必须全部为__int64,不然就错   
  13.         scanf("%I64d %I64d %I64d %I64d",&a,&b,&c,&k);  
  14.         if((b-a)==(c-b)){  
  15.             m=(c+((k-3)%M)*((c-b)%M))%M;  
  16.         }   
  17.         else{  
  18.             m=a%M;  
  19.             k=k-1;  
  20.              l=(c/b)%M;  
  21.              while(k){  
  22.                 if(k&1){//奇数   
  23.                     r=r*l%M;  
  24.                     k--;  
  25.                 }  
  26.                 else{  
  27.                     if(k==2)  
  28.                     k=0;  
  29.                     else  
  30.                 k=k>>1;  
  31.                 l=l*l%M;  
  32.                 }  
  33.              }  
  34.         }  
  35.         printf("%I64d\n",m*r*l%200907);  
  36.     }  
  37.     return 0;  
这个是出现错误的代码
#include <iostream>#include<string.h>using namespace std;int a[3],s=200907;long long pow(long long q,int n){    long long ans=a[0];    n=n-1;    while (n!=0)    {            if(n%2==1)            {            ans=((ans%s)*(q%s))%s;            }            n/=2;            q=((q%s)*(q%s))%s;    }    return ans%s;}int main(){    int n,t;    int ans;    cin>>t;    while(t--)    {        cin>>a[0]>>a[1]>>a[2]>>n;        if(a[0]+a[2]==2*a[1])        {            int d=a[2]-a[1];            ans=(a[0]+(n-1)*d%s)%s;            cout<<ans<<endl;        }        else         {            long long q=a[2]/a[1];            ans=pow(q,n);            cout<<ans<<endl;        }    }    return 0;}


0 0
原创粉丝点击