问题 C: 阶乘位数
时间限制: 1 Sec 内存限制: 128 MB
[提交] [状态]
题目描述
在很多软件中需要用到较大的整数。比如一些软件将大数用于数据安全传送的密匙或者密码编译等等。在这个问题中,你要根据给你的整数,算出这个数的阶乘的位数。
输入
含若干行整数。第一行为n,表示案例数,接下来是n行,每行一个整数m(1≤m≤10^7)。
输出
输出这些数的阶乘的位数。
样例输入 Copy
2
10
20
样例输出 Copy
7
19
这个是数学方面的定理。。。。。-->-->
#include <bits/stdc++.h>
using namespace std;
vector<int>A[200005];
int sum[200005];
const long double pi=3.1415926535897932384626433832795;
const long double e=2.71828182845904523536028747135266;
int n,m,ans;
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;++i)
{
scanf("%d",&m);
int ans=1;
if(m>3)
{
//斯特林定理,这个知识点。。。。
ans=log10(2*pi*m)/2+m*log10(m/e)+1;
}
printf("%d\n",ans);
}
return 0;
}
问题 J: Ki
时间限制: 1 Sec 内存限制: 128 MB
[提交] [状态]
题目描述
Given is a rooted tree with N vertices numbered 1 to N. The root is Vertex 1, and the i-th edge (
1≤i≤N−1) connects Vertex ai and bi.
Each of the vertices has a counter installed. Initially, the counters on all the vertices have the value 0.
Now, the following Q operations will be performed:
Operation j (1≤j≤Q): Increment by xj the counter on every vertex contained in the subtree rooted at Vertex pj.
Find the value of the counter on each vertex after all operations.
Constraints
·2≤N≤2×105
·1≤Q≤2×105
·1≤ai<bi≤N
·1≤pj≤N
·1≤xj≤104
·The given graph is a tree.
·All values in input are integers.
输入
Input is given from Standard Input in the following format:
N Q
a1 b1
:
aN−1 bN−1
p1 x1
:
pQ xQ
Print the values of the counters on Vertex 1,2,…,N after all operations, in this order, with spaces in between.
样例输入 Copy
【样例1】
4 3
1 2
2 3
2 4
2 10
1 100
3 1
【样例2】
6 2
1 2
1 3
2 4
3 6
2 5
1 10
1 10
样例输出 Copy
【样例1】
100 110 111 110
【样例2】
20 20 20 20 20 20
样例1解释
The tree in this input is as follows:
Each operation changes the values of the counters on the vertices as follows:
·Operation 1: Increment by 10 the counter on every vertex contained in the subtree rooted at Vertex 2, that is, Vertex 2,3,4. The values of the counters on Vertex 1,2,3,4 are now 0,10,10,10, respectively.
·Operation 2: Increment by 100 the counter on every vertex contained in the subtree rooted at Vertex 1, that is, Vertex 1,2,3,4. The values of the counters on Vertex 1,2,3,4 are now 100,110,110,110, respectively.
·Operation 3: Increment by 1 the counter on every vertex contained in the subtree rooted at Vertex
3, that is, Vertex 3. The values of the counters on Vertex 1,2,3,4 are now 100,110,111,110, respectively.
//这个是超时代码->->
#include <bits/stdc++.h>
using namespace std;
vector<int>A[200005];
int sum[200005];
void dfs(int a,int b)
{
sum[a]+=b;
if(!A[a].empty())
{
int si=A[a].size();
for(int j=0;j<si;j++)
{
if(!A[A[a][j]].empty())
{
dfs(A[a][j],b);
}
if(A[A[a][j]].empty())
{
sum[A[a][j]]+=b;
}
}
}
}
int main()
{
int n,q;
scanf("%d %d",&n,&q);
for(int i=1;i<n;i++)
{
int a,b;
scanf("%d %d",&a,&b);
A[a].push_back(b);
}
for(int i=1;i<=q;i++)
{
int a,b;
scanf("%d %d",&a,&b);
dfs(a,b);
}
for(int i=1;i<=n;i++)
printf("%d ",sum[i]);
return 0;
}
//第一次改进后 错误代码,其实错误原因很明显。。。
#include <bits/stdc++.h>
using namespace std;
vector<int>A[200005];
int sum[200005];
struct node
{
int x;
int y;
}pos[200005];
bool cmp(struct node a,struct node b)
{
if(a.x==b.x) return a.y<b.y;
return a.x<b.x;
}
int main()
{
int n,q;
scanf("%d %d",&n,&q);
for(int i=1; i<n; i++)
{
scanf("%d %d",&pos[i].x,&pos[i].y);
}
for(int i=1; i<=q; i++)
{
int a,b;
scanf("%d %d",&a,&b);
sum[a]+=b;
}
sort(pos+1,pos+n,cmp);
for(int i=1;i<n;i++)
{
sum[pos[i].y]+=sum[pos[i].x];
}
for(int i=1;i<=n;i++)
printf("%d ",sum[i]);
return 0;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<int>A[200005];
ll sum[200005];
void dfs(int a,int b)
{
for(int i=0;i<A[a].size();i++)
{
if(A[a][i]==b) continue;
sum[A[a][i]]+=sum[a];
dfs(A[a][i],a);
}
}
int main()
{
int n,q;
scanf("%d %d",&n,&q);
for(int i=1;i<n;i++)
{
int a,b;
scanf("%d %d",&a,&b);
A[a].push_back(b);
A[b].push_back(a);
}
for(int i=1;i<=q;i++)
{
int a,b;
scanf("%d %d",&a,&b);
sum[a]+=b;
}
dfs(1,-1);
for(int i=1;i<=n;i++)
printf("%lld ",sum[i]);
return 0;
}