hdu 2817 A sequence of numbers(快速幂取余)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2817

题目大意:给出三个数,来判断是等差还是等比数列,再输入一个n,来计算第n个数的值。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #define m 200907
 5
 6 using namespace std;
 7
 8 __int64 fun(__int64 j,__int64 k)
 9 {
10     __int64 s=1;
11     while (k)
12     {
13         if (k%2==1)
14             s=s*j%m;
15         j=(j*j)%m;
16         k/=2;
17     }
18     return s;
19 }
20 int main ()
21 {
22     __int64 t,a,b,c,n,s;
23     cin>>t;
24     while (t--)
25     {
26         cin>>a>>b>>c>>n;
27         if (b-a==c-b)
28         {
29             int d=b-a;
30             s=(a+(n-1)*d)%m;
31         }
32         else
33         {
34             int q=b/a;
35             s=(a*fun(q,n-1))%m;
36         }
37         printf ("%I64d\n",s);
38     }
39     return 0;
40 }

hdu 2817 A sequence of numbers(快速幂取余)

时间: 2024-11-13 20:47:10

hdu 2817 A sequence of numbers(快速幂取余)的相关文章

hdu 2817 A sequence of numbers(快速幂)

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

POJ1995:Raising Modulo Numbers(快速幂取余)

题目:http://poj.org/problem?id=1995 题目解析:求(A1B1+A2B2+ ... +AHBH)mod M. 大水题. #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <math.h> using namespace std; int n,mod,sum; int main() { int

HDU 4965 Fast Matrix Calculation (矩阵快速幂取模----矩阵相乘满足结合律)

http://acm.hdu.edu.cn/showproblem.php?pid=4965 利用相乘的可结合性先算B*A,得到6*6的矩阵,利用矩阵快速幂取模即可水过. 1 #include<iostream> 2 #include<stdio.h> 3 #include<iostream> 4 #include<stdio.h> 5 #define N 1010 6 #define M 1010 7 #define K 6 8 using namespa

HDU1061_Rightmost Digit【快速幂取余】

Rightmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 33161    Accepted Submission(s): 12696 Problem Description Given a positive integer N, you should output the most right digit of

poj 1845 Sumdiv (同余定理,快速幂取余)

链接:poj 1845 题意:求A^B的所有因子的和对9901取余后的值 如:2^3=8,8的因子有 1,2,4,8,所有和为15,取余后也是15 应用定理主要有三个: (1)整数的唯一分解定理: 任意正整数都有且只有一种方式写出其素因子的乘积表达式. A=(p1^k1)*(p2^k2)*(p3^k3)*....*(pn^kn)   其中pi均为素数 (2)约数和公式: 对于已经分解的整数A=(p1^k1)*(p2^k2)*(p3^k3)*....*(pn^kn) 有A的所有因子之和为 S = 

快速幂取余 [转]

转自: http://blog.csdn.net/acm_code/article/details/38270829 求a^b mod c 算法1. 首先直接地来设计这个算法: int ans=1, i; for(i=1;i<=b;i++) ans*=a; ans%=c; 这个算法的时间复杂度体现在for循环中,为O(b). 这个算法存在着明显的问题,如果a和b过大,很容易就会溢出. 那么,我们先来看看第一个改进方案:在讲这个方案之前,要先有这样一个公式: a^b mod c=(a mod c)

LightOJ - 1282 - Leading and Trailing(数学技巧,快速幂取余)

链接: https://vjudge.net/problem/LightOJ-1282 题意: You are given two integers: n and k, your task is to find the most significant three digits, and least significant three digits of nk. 思路: 后三位快速幂取余,考虑前三位. \(n^k\)可以表示为\(a*10^m\)即使用科学计数法. 对两边取对数得到\(k*log

HDU - 2817 - A sequence of numbers (快速幂取模!)

A sequence of numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3494    Accepted Submission(s): 1073 Problem Description Xinlv wrote some sequences on the paper a long time ago, they migh

HDU - 1097 - A hard puzzle (快速幂取模)

A hard puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 32633    Accepted Submission(s): 11672 Problem Description lcy gives a hard puzzle to feng5166,lwg,JGShining and Ignatius: gave a a