数的长度
Problem:65
Time Limit:1000ms
Memory Limit:65536K
Description
N! (N的阶乘) 是非常大的数,计算公式为:N! = N * (N - 1) * (N - 2) * … * 2 * 1)。现在需要知道N!有多少(十进制)位。
Input
每行输入1个正整数N。0 < N < 1000000
Output
对于每个N,输出N!的(十进制)位数。
Sample Input
1
3
32000
1000000
Sample Output
1
1
130271
5565709
Hint
Source
解题思路:
log10(100)=2
log10(199)=2.3
所以可知求阶乘的长度就是取lg的数+1
有小数的时候将double转换为int再加1
#include <bits/stdc++.h>
using namespace std;
int main()
{
long long n,k;
double ans;
while(cin>>n)
{
ans=0;
for(int i=1;i<=n;i++)
{
ans=ans+log10(i);
}
k=(long long)ans+1;(变成整数型+1)
cout<<k<<endl;
}
return 0;
}
最左边的数
Problem:66
Time Limit:1000ms
Memory Limit:65536K
Description
Given a positive integer N, you should output the leftmost digit of N^N.
Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single positive integer N(1<=N<=1,000,000,000).
Output
For each test case, you should output the leftmost digit of N^N.
Sample Input
2
3
4
Sample Output
2
2
Hint
In the first case, 3 * 3 * 3 = 27, so the leftmost digit is 2.
In the second case, 4 * 4 * 4 * 4 = 256, so the leftmost digit is 2.
Source
#include <bits/stdc++.h>
typedef long long LL;
using namespace std;
int main()
{
long long ans,m,k,n;
double x,y;
cin>>n;
while(n--)
{
cin>>k;
x=k*log10(k);
y=(LL)x;
x=x-y;
ans=pow(10.0,x);
cout<<ans<<endl;
}
return 0;
}
数字序列(循环节问题)
遥想公瑾当年,小乔初嫁了(找周期,周期不超过取余数的三倍)、、
(取余小于300考虑)
Problem:67
Time Limit:1000ms
Memory Limit:65536K
Description
A number sequence is defined as follows:
f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.
Given A, B, and n, you are to calculate the value of f(n).
Input
The input consists of multiple test cases. Each test case contains 3 integers A, B and n on a single line (1 <= A, B <= 1000, 1 <= n <= 100,000,000). Three zeros signal the end of input and this test case is not to be processed.
Output
For each test case, print the value of f(n) on a single line.
Sample Input
1 1 3
1 2 10
0 0 0
Sample Output
2
5
Hint
Source
zjcpc2004
昨日重现(1007,不能用循环节)
Problem:519
Time Limit:1000ms
Memory Limit:65536K
Description
兴安黑熊在高中学习数学时,曾经知道这样一个公式:f(n)=12+22+32+…+n2,这个公式是可以化简的,化简后的结果是啥它却忘记了,也许刚上大二的你能记得。现在的问题是想要计算f(n)对1007取余的值,你能帮帮他吗?
Input
输入数据有多组(不超过100组),每组一个数n. (1<=n <=1,000,000,000).
Output
输出f(n)对1007取余的值。
Sample Input
3
4
100
Sample Output
14
30
1005
Hint
本题4分,容易超时
Source
陈宇