2018 ICPC Asia Nakhon Pathom Regional Contest-F.Lucky Pascal Triangle(杨辉三角,递归,分治,规律)

2018 ICPC Asia Nakhon Pathom Regional Contest-F.Lucky Pascal Triangle(杨辉三角,递归,分治,规律)

题意:

问杨辉三角\([0,n]\)行有多少个数是7的倍数,\(n\leq 10^{18}\)

思路:

下图中1代表该数是7的倍数,

那么可以发现:整体是按照尺寸为\(7^k\)的方块进行规律分部的。

正难则反:

我们改为求下图0的个数,然后总个数减去即可得到1的个数。

然后按照\(7^k\)计算对应个数贡献递归求解即可。

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define chu(x)  if(DEBUG_Switch) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
void pvarr_int(int *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%d%c", arr[i], i == n ? '\n' : ' ');}}
void pvarr_LL(ll *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%lld%c", arr[i], i == n ? '\n' : ' ');}}
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
#define DEBUG_Switch 0
const ll mod = 1e9 + 7ll;
ll base[100];
ll combase[100];
ll inv2 = (mod + 1) / 2;
ll solve(int pos, ll n)
{
    if (n == 0 || pos == -1) {
        return 0ll;
    }
    ll num = n / base[pos];
    ll rm = n % base[pos];
    ll res = num*(num+1)/2%mod * combase[pos] % mod + (num + 1) % mod * solve(pos - 1, rm) % mod;
    res %= mod;
    return res;
}
int main()
{
#if DEBUG_Switch
    freopen("D:\\code\\input.txt", "r", stdin);
#endif
    //freopen("D:\\code\\output.txt","w",stdout);
    base[0] = 1;
    combase[0] = 1;
    repd(i, 1, 22) {
        base[i] = base[i - 1] * 7ll;
        combase[i] = combase[i - 1] * 28ll % mod;
    }
    int t;
    t = readint();
    int icase = 0;
    while (t--) {
        ll n = readll();
        n++;
        ll ans = n % mod;
        ans = ans * (ans + 1)/2 % mod;
        int dep;
        for (int i = 0; i <= 22; ++i) {
            if (base[i] <= n) {
                dep = i;
            } else {
                break;
            }
        }
        ans = (ans - solve(dep, n) + mod) % mod;
        printf("Case %d: %lld\n", ++icase, ans );
    }

    return 0;
}



posted @ 2020-12-24 22:46  茄子Min  阅读(135)  评论(0编辑  收藏  举报