Educational Codeforces Round 60 (Rated for Div. 2)D(思维,DP,快速幂)

#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9+7;
unordered_map<long long,long long>mp;
long long n,m;
long long dp(long long n){
    if(n<0)
        return 0;
    if(n<m)
        return 1;
    if(mp.find(n)!=mp.end())//已经存在过mp[n]的话返回就完事了
        return mp[n];
    long long mid=n/2;//二分
    long long tot=dp(mid)*dp(n-mid)%mod;//一半的每种情况都可以对应另一半的任意一种情况所以可以分解为两半相乘
    for(long long i=1;i<m;i++)//dp[n]=dp[n-1]+dp[n-m],状态转移的灵感来源
        tot=(tot+dp(mid-i)*dp(n-m-(mid-i))%mod)%mod;//n-m到n这一段的组合情况
    mp[n]=tot;//更新
    return mp[n];
}
int main(){
 scanf("%lld%lld",&n,&m);
 mp[0]=0;
 mp[1]=1;
 printf("%lld",dp(n));
 return 0;
}

原文地址:https://www.cnblogs.com/ldudxy/p/10496453.html

时间: 2024-08-30 17:40:00

Educational Codeforces Round 60 (Rated for Div. 2)D(思维,DP,快速幂)的相关文章

Educational Codeforces Round 60 (Rated for Div. 2)

A. Best Subsegment 题意 找 连续区间的平均值  满足最大情况下的最长长度 思路:就是看有几个连续的最大值 1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn= 1e5+4; 4 int a[maxn]; 5 int main(){ 6 int n; 7 scanf("%d",&n); 8 int maxnum=0; 9 for(int i=0;i<n;i++)s

Educational Codeforces Round 60 (Rated for Div. 2) E. Decypher the String

题目大意:这是一道交互题.给你一个长度为n的字符串,这个字符串是经过规则变换的,题目不告诉你变换规则,但是允许你提问3次:每次提问你给出一个长度为n的字符串,程序会返回按变换规则变换后的字符串,提问3次后你需要猜出这个字符串.解法是学习https://blog.csdn.net/baiyifeifei/article/details/87807822 这个博主的,借用了进制的思想非常巧妙. 解法:对于某个位置的来源位置我们设为x,因为26*26*26>10000,那么x可以唯一表示为x=a*26

Educational Codeforces Round 60 (Rated for Div. 2) A. Best Subsegment

time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output You are given array a 1 , a 2 ,-, a n a1,a2,-,an . Find the subsegment a l , a l+1 ,-, a r al,al+1,-,ar ( 1≤l≤r≤n 1≤l≤r≤n ) with maximum arithmet

Educational Codeforces Round 60 (Rated for Div. 2) D. Magic Gems(矩阵快速幂)

题目传送门 题意: 一个魔法水晶可以分裂成m个水晶,求放满n个水晶的方案数(mol1e9+7) 思路: 线性dp,dp[i]=dp[i]+dp[i-m]; 由于n到1e18,所以要用到矩阵快速幂优化 注意初始化 代码: #include<bits/stdc++.h> using namespace std; #define mod 1000000007 typedef long long ll; #define MAX 105 const int N=105;//矩阵的大小 int T; ll

Educational Codeforces Round 60 (Rated for Div. 2)E(思维,哈希,字符串,交互)

#include <bits/stdc++.h>using namespace std;int main(){ string t; cin>>t; int n=t.size(); string s1(n,'a'),s2(n,'a'),s3(n,'a'); for(int i=0;i<n;i++){  s1[i]=char('a'+(i%26));//从a到z循环  s2[i]=char('a'+((i/26)%26));//第i位为(i/26)%26+'a',保证了26*26

Educational Codeforces Round 59 (Rated for Div. 2) E 区间dp + 状态定义

https://codeforces.com/contest/1107/problem/E 题意 给出01字符串s(n<=100),相邻且相同的字符可以同时消去,一次性消去i个字符的分数是\(a[i]\),问消去s最多能得到多少分数 题解 实质是安排消去次序使得分数最大,第一步采取的行动是递归边界 因为只有01串,所以s被分成了一段一段,考虑段处理 预处理出一次性消去i个字符的最大分数\(f[i]\) 定义\(dp[l][r][cnt]\)为消去第l到第r段加上cnt个字符和第l段相同得到的最大

Educational Codeforces Round 80 (Rated for Div. 2)C(DP)

1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace std; 4 const long long mod = 1e9+7; 5 long long pre[1007][1007],temp[1007][1007]; 6 int main(){ 7 ios::sync_with_stdio(false); 8 cin.tie(NULL); 9 cout.tie(NULL); 10 int n,m;

Educational Codeforces Round 36 (Rated for Div. 2)

Educational Codeforces Round 36 (Rated for Div. 2) F. Imbalance Value of a Tree You are given a tree T consisting of n vertices. A number is written on each vertex; the number written on vertex i is ai. Let's denote the function I(x,?y) as the differ

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars There are n pillars aligned in a row and numbered from 1 to n. Initially each pillar contains exactly one disk. The i-th pillar contains a disk having radius ai. You can move these disks