数字序列
时间限制:2000/1000 MS(Java / Others)内存限制:65536/32768 K(Java / Others)
总共提交数量:183301已接受提交:45608
问题描述
一个数字序列被定义如下:
F(1)= 1,F(2)= 1,F(N)=(A * F(N - 1)+ B * F(N - 2))模7.
鉴于A,B和n,你要计算f(n)的值。
输入
输入由多个测试用例组成。每个测试用例在一行上包含3个整数A,B和n(1 <= A,B <= 1000,1 <= n <= 100,000,000)。三个零信号表示输入结束,这个测试用例不被处理。
产量
对于每个测试用例,在一行上打印f(n)的值。
Sample Input
1 1 3
1 2 10
0 0 0
Sample Output
2
5
算法:直接用递归算会超时
猜想最终结果存在一个循环周期,需要找规律
(A*F(n-1))%7结果: 0 12 3 4 5 6 7种结果
(B*F(n-2))%7结果: 0 12 3 4 5 6 7种结果
所以,猜测最后结果的最大周期为49
所以,在49的范围内遍历,寻找到周期,这样就不会超时。
代码:
#include <stdio.h>
int main(){
int a, b, n;
while (true) {
scanf("%d %d %d",&a, &b, &n);
if (a == 0 &&b == 0 && n == 0) {
break;
}
int fn[200];
fn[1]= 1;
fn[2]= 1;
int i;
for (i = 3; i <= 52;i++) { //最大周期49
fn[i]= (a*fn[i - 1] + b*fn[i - 2]) % 7;
if (fn[i - 1] == 1&& fn[i] == 1) { //找到周期了
break;
}
}
i= i - 2; //周期是i-2
n= n % i;
fn[0]= fn[i];
printf("%d\n", fn[n]);
//for (int i = 1; i < 49; i++) {
// printf("fn[%d]=%d\n",i,fn[i]);
//}
}
return 0;
}
The LastPractice
Time Limit: 2000/1000 MS(Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11268 Accepted Submission(s): 2577
Problem Description
Tomorrow iscontest day, Are you all ready?
We have been training for 45 days, and all guys must be tired.But , you are solucky comparing with many excellent boys who have no chance to attend theProvince-Final.
Now, your task is relaxing yourself and making the last practice. I guess thatat least there are 2 problems which are easier than this problem.
what does this problem describe?
Give you a positive integer, please split it to some prime numbers, and you cangot it through sample input and sample output.
Input
Input filecontains multiple test case, each case consists of a positive integern(1<n<65536), one per line. a negative terminates the input, and itshould not to be processed.
Output
For each test caseyou should output its factor as sample output (prime factor must come forthascending ), there is a blank line between outputs.
Sample Input
60
12
-1
Sample Output
Case 1.
2 2 3 1 5 1
Case 2.
2 2 3 1
Hint
60=2^2*3^1*5^1
题意:将一个正整数分解成一些质数
算法:暴力枚举
一个数n可以表示为若干个质数的乘积,那么这个n的因子必然可以是质数或者质数的乘积
例如 12 因子为 1 2 3 4 6
4 6是合数,不是质数,但是可以表示为 2*22*3 质数的乘积
那么怎么分解n呢?
从2开始遍历到n(1就不用管了,1还是1)
定义一个变量j记录幂
当i是n的因数,n/=i ,幂次直到i不再是n的因数,,i^j为n的因数。
输出i j
比如 60=2^2*3^1*5^1
13=1*13^1
128=2^7
代码:
#include<stdio.h>
#include<iostream>
usingnamespacestd;
intmain() {
int n, j; //j记录幂
int k = 1;
int p = 0;
while (scanf("%d", &n)&& n>1) {
if (p) {
printf("\n");
}
int m = n;
printf("Case %d.\n", k++);
if (n == 1) {
printf("1\n\n");
}
for (int i = 2; i <= m;i++) {
j= 0;
while (n%i == 0) { //12= 2^2*3^1
n= n / i;
j++; //幂次+1
}
if (j != 0) {
cout<< i << " " << j<< " ";
}
}
printf("\n");
p= 1;
}
return0;
}
循环数比较
爱奇艺2018秋招前端卷(一)
对于任意两个正整数x和k,我们定义repeat(x, k)为将x重复写k次形成的数,例如repeat(1234, 3) =123412341234,repeat(20,2) = 2020.
牛牛现在给出4个整数x1, k1, x2, k2, 其中v1 = (x1, k1), v2 = (x2, k2),请你来比较v1和v2的大小。
输入描述:
输入包括一行,一行中有4个正整数x1, k1, x2, k2(1 ≤ x1,x2 ≤ 10^9, 1 ≤ k1,k2 ≤ 50),以空格分割
输出描述:
如果v1小于v2输出"Less",v1等于v2输出"Equal",v1大于v2输出"Greater".
示例1
输入
1010 3 101010 2
输出
Equal
算法:将输入转化为字符串(因为超过整数表达范围)再进行比较
代码:
#include <stdio.h>
#include <string>
#include <iostream>
#include <stdlib.h>
#include <string.h>
usingnamespacestd;
stringtranslate(intm, intn) {
chars[10000];
sprintf(s, "%d", m); //数字转字符串
charnewS[100];
strcpy(newS, s);
for (inti = 0; i < n-1; i++) {
strcat(s,newS);
}
returns;
}
intmain() {
intx1, k1, x2, k2;
scanf("%d %d %d%d", &x1, &k1, &x2, &k2);
strings1 = translate(x1, k1);
strings2 = translate(x2, k2);
intlenA = s1.length();
intlenB = s2.length();
if (lenA > lenB) {
printf("Greater\n");
}
if (lenA < lenB) {
printf("Less\n");
}
if (lenA == lenB) {
intflag = 1;
for (inti = 0; i < lenA; i++) {
if (s1[i] != s2[i]) {
flag = 0;
break;
}
}
if (flag == 1) {
printf("Equal\n");
}
if (flag == 0) {
for (inti = 0; i < lenA; i++) {
if (s1[i] > s2[i]) {
printf("Greater\n");
break;
}
if (s1[i] < s2[i]) {
printf("Less\n");
break;
}
}
}
}
return0;
}