Lucky Pascal Triangle Gym - 102091F(分块+递归)

题解:是以7的倍数为块,进行分配,打出表后,观察可知非7的倍数更好用递归求解。

​
​
#include"bits/stdc++.h"
using namespace std;
typedef long long LL;
const LL mod = 1e9 + 7;
LL g[33], res[33];

LL inv2;
LL dfs(int dep, LL n){
    if(dep == -1 || n == 0) return 0;
    LL num = n/g[dep];
    LL m = n%g[dep];
    LL ans = (num+1)*num/2*res[dep]%mod + (dfs(dep-1,m)*(num+1)%mod);
    return ans%mod;
}

int main() {
	inv2 = (mod+1)/2;
	res[0] = g[0] = 1;
    for(int i = 1; i <= 22; i++){
        g[i] = g[i-1]*7;
        res[i] = res[i-1]*28%mod;
    }
	int T, cas = 0;
	scanf("%d", &T);
	LL n;
	while(T--){
        scanf("%lld",&n);
        ++n;
        int dep = 0;
        for(int i = 0; i <= 22; i++)
        if(g[i] > n){
            dep = i-1;
            break;
        }
        LL t = n%mod;
        LL ans = t*(t+1)%mod*inv2%mod - dfs(dep,n);
        printf("Case %d: %lld\n",++cas,(ans%mod+mod)%mod);
	}
	return 0;
}

​

​

 

你可能感兴趣的:(规律打表)