SCU 4426 Counting_3 (dp)

题目链接

4426: Counting_3

Submit your solution     Discuss this problem     Best solutions

Time Limit: 1000ms

Description

Define f(0)=1 and f(n) to be the number of different ways n can be expressed as a sum of integer powers of 3 using each power no more than 3 times.
For example, f(9)=3 since there are 3 ways to express 9:
    1+1+1+3+3
    3+3+3
    9
For a given n, please calculate f(n).

Input

Line 1: T, indicating the number of test cases.
Line 2 - T+1: n (1 <= n <= 10^18)

Output

For each case, output f(n) in a line.

Sample Input

2
9
10

Sample Output

3
2

Author

huangshenno1 


题意:把一个数n,分成若干个 3的某次方的数的和,每个数不能超过3个。求有多少种分法?

题解:显然要先把n转换为三进制的数,这显然是一种满足条件的方案,每一位最大为2。接下来就容易想到dp了。

用dp[i][j] 表示最高位到第i+1位都合法,第i位为j 的方案数 。转移见代码:

#include<cstdio>
#include<cstring>
#include<set>
#include<iostream>
#include<map>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<cctype>
#include<algorithm>
#define inff 0x3fffffff
#define nn 8100000
#define mod 1000000007
typedef long long LL;
using namespace std;
LL n;
int a[50];
LL dp[50][5];
int main()
{
    int t,i,j;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld",&n);
        int ix=0;
        while(n)
        {
            a[ix++]=n%3;
            n/=3;
        }
        memset(dp,0,sizeof(dp));
        dp[ix][0]=1;
        for(i=ix-1;i>=0;i--)
        {
            for(j=0;j<=3;j++)
            {
                dp[i][a[i]]+=dp[i+1][j];
                if(a[i]<2)
                    dp[i][a[i]+3]+=dp[i+1][j+1];
            }
        }
        printf("%lld\n",dp[0][0]+dp[0][1]+dp[0][2]+dp[0][3]);
    }
    return 0;
}




这段代码是一个汇编代码片段,用于处理一个叫做`TRAP_Internal_Protection_0`的函数。让我为你解释每一行的意思: 1. `.section .cpu0_traptab_001,"ax",@progbits`:这行指令定义了一个名为`.cpu0_traptab_001`的段(section),用于存放程序的可执行代码。`"ax"`表示该段是可执行的,并且被放置在可执行文件的`.text`段中。 2. `.align 5`:这行指令将当前位置对齐到2^5=32字节的边界。这通常用于确保代码的指令对齐。 3. `.global TRAP_Internal_Protection_0`:这行指令将`TRAP_Internal_Protection_0`标记为全局符号,使得它可以在其他文件中使用。 4. `.type TRAP_Internal_Protection_0, @function`:这行指令将`TRAP_Internal_Protection_0`标记为函数类型。 5. `TRAP_Internal_Protection_0:`:这行指令定义了函数的入口点。 6. `.ifdef BREAK_AT_TRAP`:这行指令用于条件编译,只有当预处理器定义了`BREAK_AT_TRAP`宏时才会执行下面的指令。 7. `DEBUG`:这行指令是一个占位符,表示在`BREAK_AT_TRAP`宏被定义时,在这里插入调试代码。 8. `.endif`:这行指令结束条件编译块。 9. `movh.a %a15, hi:(SCU_Internal_Protection_Trap_C0)`:这行指令将高位地址值加载到寄存器`%a15`中。`movh.a`是一个汇编指令,用于将高位地址加载到寄存器。 10. `lea %a15,[%a15] lo:(SCU_Internal_Protection_Trap_C0)`:这行指令将低位地址值加载到寄存器`%a15`中。`lea`是一个汇编指令,用于计算地址并将其加载到寄存器。 11. `ji %a15`:这行指令进行无条件跳转,跳转到寄存器`%a15`所指向的地址。 12. `rfe`:这行指令执行返回异常(return from exception)指令,用于从异常处理程序返回到正常执行流程。 请注意,这段代码是使用特定的汇编语言编写的,具体的指令和语法可能与不同的处理器和工具链有关。上述解释是基于一些常见的汇编语言约定和假设。如果你需要更详细的解释,请提供更多上下文或相关信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值