HDU 2817 A sequence of numbers (快速幂)

本文介绍了一种解决数学序列中查找特定位置数值的问题。针对等差或等比数列,给定了前三个数和目标位置K,通过算法计算出K位置上的数,并确保结果在特定模数下有效。
部署运行你感兴趣的模型镜像

A sequence of numbers

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6823    Accepted Submission(s): 2073


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

#include<iostream>
#include<cmath>
using namespace std;

typedef long long ll;

int mod = 200907;

ll mod_pow(ll x, ll n)
{
	ll y = 1;
	while(n > 0)
	{
		if(n & 1)    //位运算 判断二进制最后一位是否为1 即是否为奇数
			y = y * x % mod;
		x = x * x % mod;
		n >>= 1;    //按位向右移动 即除以2
	}
	return y;
}

int main()
{
	int n;
	cin >> n;
	while(n--)
	{
		ll a, b, c, k;
		cin >> a >> b >> c >> k;
		if(b - a == c - b)
		{
			ll ans = a % mod + (b - a) * (k - 1) % mod;
			cout << ans % mod << endl;
		}
		else
		{
			ll ans = a * mod_pow((b / a), (k - 1));
			cout << ans % mod << endl;
		}
	}
	return 0;
}

您可能感兴趣的与本文相关的镜像

Dify

Dify

AI应用
Agent编排

Dify 是一款开源的大语言模型(LLM)应用开发平台,它结合了 后端即服务(Backend as a Service) 和LLMOps 的理念,让开发者能快速、高效地构建和部署生产级的生成式AI应用。 它提供了包含模型兼容支持、Prompt 编排界面、RAG 引擎、Agent 框架、工作流编排等核心技术栈,并且提供了易用的界面和API,让技术和非技术人员都能参与到AI应用的开发过程中

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值