ZOJ3549 Little Keng(快速幂)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud

Little Keng


Time Limit: 2 Seconds      Memory Limit: 65536 KB


Calculate how many 0s at the end of the value below:
1n + 2n + 3n + ... + mn

Input

There are multiple cases. For each cases, the input containing two integers mn. (1 <= m <= 100 , 1 <= n <= 1000000)

Output

One line containing one integer indicatint the number of 0s.

Sample Input

4 3

Sample Output

2

看完这题,感觉0的位数不会很多,拿Java大数跑了一部分数据,发现就直接取1e9没什么问题,然后就直接搞了。

 1 /**
 2  * code generated by JHelper
 3  * More info: https://github.com/AlexeyDmitriev/JHelper
 4  * @author xyiyy @https://github.com/xyiyy
 5  */
 6
 7 #include <iostream>
 8 #include <fstream>
 9
10 //#####################
11 //Author:fraud
12 //Blog: http://www.cnblogs.com/fraud/
13 //#####################
14 //#pragma comment(linker, "/STACK:102400000,102400000")
15 #include <iostream>
16 #include <sstream>
17 #include <ios>
18 #include <iomanip>
19 #include <functional>
20 #include <algorithm>
21 #include <vector>
22 #include <string>
23 #include <list>
24 #include <queue>
25 #include <deque>
26 #include <stack>
27 #include <set>
28 #include <map>
29 #include <cstdio>
30 #include <cstdlib>
31 #include <cmath>
32 #include <cstring>
33 #include <climits>
34 #include <cctype>
35
36 using namespace std;
37 #define rep2(X, L, R) for(int X=L;X<=R;X++)
38 typedef long long ll;
39
40 //
41 // Created by xyiyy on 2015/8/5.
42 //
43
44 #ifndef ICPC_QUICK_POWER_HPP
45 #define ICPC_QUICK_POWER_HPP
46 typedef long long ll;
47
48 ll quick_power(ll n, ll m, ll mod) {
49     ll ret = 1;
50     while (m) {
51         if (m & 1) ret = ret * n % mod;
52         n = n * n % mod;
53         m >>= 1;
54     }
55     return ret;
56 }
57
58 #endif //ICPC_QUICK_POWER_HPP
59
60 class TaskA {
61 public:
62     void solve(std::istream &in, std::ostream &out) {
63         int m, n;
64         while (in >> m >> n) {
65             ll ans = 0;
66             rep2(i, 1, m)ans += quick_power(i, n, 1e9);
67             int cnt = 0;
68             while (ans) {
69                 if (ans % 10 != 0)break;
70                 cnt++;
71                 ans /= 10;
72             }
73             out << cnt << endl;
74         }
75     }
76 };
77
78 int main() {
79     std::ios::sync_with_stdio(false);
80     std::cin.tie(0);
81     TaskA solver;
82     std::istream &in(std::cin);
83     std::ostream &out(std::cout);
84     solver.solve(in, out);
85     return 0;
86 }
时间: 2024-08-06 01:45:48

ZOJ3549 Little Keng(快速幂)的相关文章

BNUOJ 34985 Elegant String 2014北京邀请赛E题 矩阵快速幂

题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34985 题目大意:问n长度的串用0~k的数字去填,有多少个串保证任意子串中不包含0~k的某一个全排列 邀请赛上A的较多的一道题,比赛的时候死活想不出,回来之后突然就想通了,简直..... = =! 解题思路: 对于所有串我们都只考虑末尾最多有多少位能构成全排列的一部分(用l来表示),即最多有多少位不重复的数字出现,将问题转化为求末尾最多有k位能构成全排列的串的总数量 假设k为5,有一个

矩阵快速幂刷题系列

来源自http://blog.csdn.net/chenguolinblog/article/details/10309423 hdu 1575 Tr A Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5587    Accepted Submission(s): 4200 Problem Description A为一个方阵,则Tr

快速幂取模(POJ 1995)

http://poj.org/problem?id=1995 以这道题来分析一下快速幂取模 a^b%c(这就是著名的RSA公钥的加密方法),当a,b很大时,直接求解这个问题不太可能 利用公式a*b%c=((a%c)*b)%c 每一步都进行这种处理,这就解决了a^b可能太大存不下的问题,但这个算法的时间复杂度依然没有得到优化 由此可以用快速幂算法优化: http://www.cnblogs.com/qlky/p/5020402.html 再结合取模公式: (a + b) % p = (a % p

HDU 1757 A Simple Math Problem (矩阵快速幂)

[题目链接]:click here~~ [题目大意]: If x < 10 f(x) = x. If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 * f(x-10); 问f(k)%m的值. [思路]:矩阵快速幂,具体思路看代码吧,注意一些细节. 代码: #include<bits/stdc++.h> using namespace std; typedef long long LL; const

快速幂及快速幂取模

快速幂顾名思义,就是快速算某个数的多少次幂.其时间复杂度为 O(log?N), 与朴素的O(N)相比效率有了极大的提高.——bybaidu 快速幂可以用位运算这个强大的工具实现. 代码: 1 int pow(int a,int b) 2 { 3 int ans=1; 4 while(b!=0) 5 { 6 if(b&1) 7 ans*=a; 8 a*=a; 9 b>>=1; 10 } 11 return ans; 12 } 快速幂取模需要记住一个定理:积的取模等于取模积的取模:算法是蒙

Codeforces Round #291 (Div. 2) E - Darth Vader and Tree (DP+矩阵快速幂)

这题想了好长时间,果断没思路..于是搜了一下题解.一看题解上的"快速幂"这俩字,不对..这仨字..犹如醍醐灌顶啊...因为x的范围是10^9,所以当时想的时候果断把dp递推这一方法抛弃了.我怎么就没想到矩阵快速幂呢.......还是太弱了..sad..100*100*100*log(10^9)的复杂度刚刚好. 于是,想到了矩阵快速幂后,一切就变得简单了.就可以把距离<=x的所有距离的点数都通过DP推出来,然后一个快速幂就解决了. 首先DP递推式很容易想到.递推代码如下: for(

NYOJ127 星际之门(一)(最小生成数的个数+快速幂)

题目描述: http://acm.nyist.net/JudgeOnline/problem.php?pid=127 可以证明,修建N-1条虫洞就可以把这N个星系连结起来. 现在,问题来了,皇帝想知道有多少种修建方案可以把这N个星系用N-1条虫洞连结起来? 输入 第一行输入一个整数T,表示测试数据的组数(T<=100) 每组测试数据只有一行,该行只有一个整数N,表示有N个星系.(2<=N<=1000000) 输出 对于每组测试数据输出一个整数,表示满足题意的修建的方案的个数.输出结果可能

POJ 3233 - Matrix Power Series ( 矩阵快速幂 + 二分)

POJ 3233 - Matrix Power Series ( 矩阵快速幂 + 二分) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; #define MAX_SIZE 30 #define CLR( a, b ) memset( a, b, sizeof(a) ) int MOD = 0; int n, k; st

HDU 4990 Reading comprehension(找规律+矩阵快速幂)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4990 Problem Description Read the program below carefully then answer the question. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include<iostream> #include