zoukankan      html  css  js  c++  java
  • HDU 2817 A sequence of numbers

    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): 1594    Accepted Submission(s): 494

    Problem 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
     
    2
    1 2 3 5
    1 2 4 5
     
    Sample Output
     
    5
    16
     
    Source
     
    Recommend
    gaojie
     
     
     
    给出数列的前三项,数列要么是等差数列、要么是等比数列,求第K项。
    很简单,注意细节处理。
    注意数据类型的溢出,要用long long,中间过程防止溢出
     
     
    #include<stdio.h>
    
    #define MOD 200907
    
    long long mod_exp(int a,int b,int n){
        long long ans=1;
        for(;b;b>>=1,a=(long long)(((long long)a)*a)%n)
            if(b&1)
                ans=(long long)(((long long)ans)*a)%n;
        return ans;
    }
    
    int main(){
        int t;
        scanf("%d",&t);
        while(t--){
            long long a,b,c,k;
            scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&k);
            if(a+c==2*b){
                long long d=(long long)(b-a);
                int ans=(a%MOD+((k-1)%MOD)*d%MOD)%MOD;
                printf("%d\n",ans);
            }else{
                long long q=(long long)(b/a);
                long long tmp=mod_exp(q,k-1,MOD);
                int ans=(a*tmp)%MOD;
                printf("%d\n",ans);
            }
        }
        return 0;
    }
  • 相关阅读:
    定义和使用EL函数
    在Java Web程序中使用Hibernate
    JDBC在Java Web中的应用——分页查询
    JDBC调用存储过程
    使用navicat工具创建MySQL存储过程
    JDBC操作数据库的批处理
    JDBC操作数据库
    Servlet监听器统计在线人数
    Servlet字符编码过滤器
    Servlet过滤器创建与配置
  • 原文地址:https://www.cnblogs.com/jackge/p/2837940.html
Copyright © 2011-2022 走看看