51nod一级算法题
1001数组中和等于k的数对:对数组排序,枚举其中一个数,然后二分对应的另一个数。
1002数塔取数问题:初等的动态规划,自底向上进行更新就好。
1003阶乘后面0的数量:分析可以发现,只需要统计5,5^2,5^3...的倍数在n中出现的次数,最后相加就是结果。
#include<iostream>
#include<cstring>
#include<math.h>
#include<stdlib.h>
#include<cstring>
#include<cstdio>
#include<utility>
#include<algorithm>
#include<map>
using namespace std;
typedef long long ll;
const int Max = 5*1e4+5;
const int mod = 1e9+7;
const int Hash = 10000;
const int INF = 1<<30;
int n;
int arr[Max];
int main( )
{
//freopen("input.txt", "r", stdin);
while(~scanf("%d", &n))
{
int ans = 0;
ll k = 5;
while(n/k)
{
ans += n/k;
k *= 5;
}
cout<<ans<<endl;
}
return 0;
}
1004n^n的末位数字:末位数字只和末位数字有关,简单的快速幂即可解,logn的复杂度。
#include<iostream>
#include<cstring>
#include<math.h>
#include<stdlib.h>
#include<cstring>
#include<cstdio>
#include<utility>
#include<algorithm>
#include<map>
using namespace std;
typedef long long ll;
const int Max = 5*1e4+5;
const int mod = 1e9+7;
const int Hash = 10000;
const int INF = 1<<30;
int n;
int arr[Max];
int main( )
{
//freopen("input.txt", "r", stdin);
while(~scanf("%d", &n))
{
int k = n%10;
int ans = k, t = 1;
while(n>1)
{
if(n%2)
t = (t*ans)%10;
ans = (ans*ans)%10;
n /= 2;
}
ans = (ans*t)%10;
cout<<ans<<endl;
}
return 0;
}
1009数字1的数量:将数拆分为位,统计每一位出现1的数