codeforces 327C Magic Five
来源:互联网 发布:ubuntu debian 知乎 编辑:程序博客网 时间:2024/05/16 07:11
题意:
给了一个数字串s,和正整数k。k是s重复的遍数。
求有多少种不同的删除元素的方法,使得删除后的数字时5的倍数。
答案mod 1e9+7
思路:
(PS:此题有优化的DP解法, 但是看不懂怎么用矩阵搞的。。)
先解决不存在周期的情况。
考虑s[i]=0 或 5,将它右边的数全部删去,即将它变成末位。
则答案增加
答案则是
然后考虑k个周期,可以发现是一个等比数列求和,公比是
答案写成
因为M是素数,所以可以把等比数列求和公式写出来然后用费马小定理解决a/b Mod M
但是,等比数列是可以用类似快速幂的方法计算的。
#include<bits/stdc++.h>using namespace std;#define SPEED_UP iostream::sync_with_stdio(false);#define FIXED_FLOAT cout.setf(ios::fixed, ios::floatfield);#define rep(i, s, t) for(int (i)=(s);(i)<=(t);++(i))#define urep(i, s, t) for(int (i)=(s);(i)>=(t);--(i))#define in_bound(l, r, i) (l)<=(i)&&(i)<(r)#define pb push_backtypedef long long LL;const int inf = INT_MAX/2;const int Maxn = 4005;const LL Mod = 1000000007;string s;LL k, p;LL opt_pow_n(LL x, unsigned int n){ LL pw = 1; while(n > 0){ if (n&1) pw = pw*x%Mod; x = x*x%Mod; n = n >> 1; } return pw;}LL go(LL n) { if (n == 1) return 1; LL tmp = go(n/2); if (n&1) { return ((tmp+opt_pow_n(p, n/2)*tmp%Mod)%Mod+opt_pow_n(p, n-1))%Mod; } else { return (tmp+opt_pow_n(p, n/2)*tmp%Mod)%Mod; }}LL solve() { LL ans = 0, n = s.length(); p = opt_pow_n(2, n); LL base = go(k); //cout << "sum of gp: " << base << endl; rep(i, 0, n-1) if (s[i] == '5' || s[i] == '0') { LL a0 = opt_pow_n(2, i); ans = (ans+a0*base%Mod)%Mod; } return ans;}int main() {#ifndef ONLINE_JUDGE freopen("input.in", "r", stdin);#endif SPEED_UP cin >> s >> k; cout << solve() << endl; return 0;}
0 0
- codeforces 327C Magic Five
- codeforces 327C. Magic Five
- 【数论】codeforces 327C Magic five
- CodeForces 327C Magic Five 题解&代码
- codeforces 327C Magic Five 分数形式整数取余
- CodeForces 327C Magic Five 二分求和 或 矩阵求和
- codeforces #327C Magic Five 矩阵快速幂加费马小定理加逆元
- CodeForces 327 C.Magic Five(快速幂)
- codeforces Magic Five
- Magic Five CodeForces
- C. Magic Five
- CodeForces Round #191 (327C) - Magic Five 等比数列求和的快速幂取模
- CodeForces Round #191 (327C) - Magic Five 乘法逆元与费马小定理
- CodeForces Round #191 (327C) - Magic Five 乘法逆元与费马小定理
- 【codeforces #327C】 Magic Five 矩阵高速幂加等比加逆元
- codeforces-327C Magic Five(等比数列求和+快速幂+逆元)
- Codeforces Round #191 (Div. 2) C magic five
- Codeforces Round #191 (Div. 2) C. Magic Five
- Xamarin.Android 如何使用Assets目录下的文件
- JAVA中的接口和抽象类(转)
- poj 2195//hdu 1533 Going Home 最小费用流(spfa)
- error: src refspec XXX matches more than one
- 基于TMS320DM814xDaVinci 中VPSS(视频处理子系统)分析
- codeforces 327C Magic Five
- 执行pod install还是pod update都卡在了Analyzing dependencies不动
- nmap推荐扫描选项
- maven tomcat plugin 热部署
- nginx--linux下安装nginx
- log4j的MDC,DNC配置以及应用
- iPhone设置的尺寸
- Tomcat的安装
- 18.Spring_声明式事务