九度oj 1442
2015-07-30 13:27
369 查看
题目描述:
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.
输入:
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 one line for each test case, that is, the K-th number module (%) 200907.
样例输入:
样例输出:
#include <stdio.h>
#define M 200907
long long fun1(long long data[], int k){
long long p = data[1] - data[0];
return ((data[0] % M) + (((k-1) % M) * (p % M)) % M) % M;
}
long long fun2(long long data[], int k){
long long p = data[1] / data[0];
long long ans = data[0];
--k;
while (k != 0){
if (k % 2 == 1){
ans = (ans * p) % M;
}
k /= 2;
p = (p * p) % M;
}
return ans;
}
long long KthNumber(long long data[], int k){
int flag;
if ((data[1] - data[0]) == (data[2] - data[1]))
flag = 0;
else
flag = 1;
if (flag == 0){
return fun1(data, k);
}
else{
return fun2(data, k);
}
}
int main(void){
int n;
long long data[3];
int k;
int i;
while (scanf ("%d", &n) != EOF){
while (n-- != 0){
for (i=0; i<3; ++i){
scanf ("%lld", &data[i]);
}
scanf ("%d", &k);
printf ("%d\n", KthNumber (data, k));
}
}
return 0;
}
此代码来自:http://blog.csdn.net/jdplus/article/details/19474255
注:本人实在想不出解决代码。还请各位大神指导啊!!!!!
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.
输入:
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 one line for each test case, that is, the K-th number module (%) 200907.
样例输入:
2 1 2 3 5 1 2 4 5
样例输出:
516
#include <stdio.h>
#define M 200907
long long fun1(long long data[], int k){
long long p = data[1] - data[0];
return ((data[0] % M) + (((k-1) % M) * (p % M)) % M) % M;
}
long long fun2(long long data[], int k){
long long p = data[1] / data[0];
long long ans = data[0];
--k;
while (k != 0){
if (k % 2 == 1){
ans = (ans * p) % M;
}
k /= 2;
p = (p * p) % M;
}
return ans;
}
long long KthNumber(long long data[], int k){
int flag;
if ((data[1] - data[0]) == (data[2] - data[1]))
flag = 0;
else
flag = 1;
if (flag == 0){
return fun1(data, k);
}
else{
return fun2(data, k);
}
}
int main(void){
int n;
long long data[3];
int k;
int i;
while (scanf ("%d", &n) != EOF){
while (n-- != 0){
for (i=0; i<3; ++i){
scanf ("%lld", &data[i]);
}
scanf ("%d", &k);
printf ("%d\n", KthNumber (data, k));
}
}
return 0;
}
此代码来自:http://blog.csdn.net/jdplus/article/details/19474255
注:本人实在想不出解决代码。还请各位大神指导啊!!!!!
相关文章推荐
- spring-hibernate 简单dao
- 百度地图的使用
- 挑战世界顶级十七项编程
- 使用total commander 复制文件名
- 配置SharePoint搜索服务
- LeetCode#12 Integer to Roman
- Ubuntu 开启SSH 以及LAMP环境安装
- 每个程序员和设计师可做的10项运动
- 谱减算法的缺点和过减因子、谱下限的关系
- oracle中执行计划中的cardinality
- LInux last命令详解
- 读书笔记17:桥接模式
- 读书笔记18:命令模式
- LInux last命令详解
- 打印最优路径
- Unable to satisfy the following requirements解决方案
- Python里如何实现C中switch...case的功能
- 充实的每一天
- CharSequence与String的区别是什么
- js 变量、函数提升