牛客网暑期ACM多校训练营(第四场)C(Chiaki Sequence Reloaded)


题目描述

Chiaki is interested in an infinite sequence a1, a2, a3, ..., which defined as follows:

Chiaki would like to know the sum of the first n terms of the sequence, i.e. . As this number may be very large, Chiaki is only interested in its remainder modulo (109 + 7).

输入描述:

There are multiple test cases. The first line of input contains an integer T (1 ≤ T ≤ 105), indicating the number of test cases. For each test case:
The first line contains an integer n (1 ≤ n ≤ 1018).

输出描述:

For each test case, output an integer denoting the answer.

示例1

输入

10
1
2
3
4
5
6
7
8
9
10

输出

0
1
2
2
4
4
6
7
8
11

题意:根据所给公式求绝对值之和

思路:官方题解

直接数位dp记忆化搜索。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
using namespace std;
const int mod=1e9+7;
typedef long long LL;
LL dp[70][3][140];
int dig[70];
int judge (int i,int j)
{
    return i==j?1:-1;
}
LL dfs(int pos,int limit,int num,int front)
{
    if (pos<0) return abs(70-num);
    if (!limit&&dp[pos][front][num]!=-1) return dp[pos][front][num];
    int end=limit?dig[pos]:1;
    LL res=0;
    for (int i=0;i<=end;i++)
    {
        if (front==2&&i==1) res+=dfs(pos-1,limit&(i==end),num,i);
        else if (front==2&&i==0)    res+=dfs(pos-1,limit&(i==end),num,2);
        else res+=dfs(pos-1,limit&(i==end),num+judge(i,front),i);
        res%=mod;
    }
    if (!limit)
        dp[pos][front][num]=res;
    return res;
}
int main()
{
    memset(dp,-1,sizeof(dp));
    LL n;
    LL t;
    scanf("%lld",&t);
    while (t--)
    {
        scanf("%lld",&n);
        int len=0;
        while (n) dig[len++]=n&1, n>>=1;
        printf("%lld\n",dfs(len-1,1,70,2));
    }  
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值