5J.炫酷数学(C++)

版权声明:本文为博主原创,未经博主允许不得转载 https://blog.csdn.net/Sherry_Yue/article/details/87391413

炫酷数学(C++)

点击做题网站链接

题目描述
小希最近想知道一个东西,就是A+B=A|B(其中|为按位或)的二元组有多少个。
当然,直接做这个式子对小希来说太难了,所以小希改变了一些条件,她仅想知道其中A,B<N的情况,其中N为2的幂次。
当然,(A=1,B=0)和(A=0,B=1)被认为是不同的二元组。

输入描述:
第一行输入一个非负整数M。
N = 2 M , M 100 N=2^M,M≤100
即2的M次为N。

输出描述:
一个整数ans,对998244353取模。

示例1
输入

0

输出
1

示例2
输入

71

输出
588378066

题目思路:

几乎是一道纯数学题。
考虑每一位,只有在(0,0)(0,1)(1,0)的三种情况时满足条件。
根据乘法原理,答案即为3^M

解题代码1:

#include <iostream>
using namespace std;
const int mod = 998244353;
 
int main()
{
	ios::sync_with_stdio(false);//加速
    int M;
    long long ans = 1;
    cin >> M;
    for(int i=0;i<M;++i)
    {
        ans *= 3;
        ans %= mod;
    }
    cout << ans << endl;
}

解题代码2:

#include <iostream>
using namespace std;
const int mod = 998244353;

long long M,ans[105];
 
int main()
{
    ios::sync_with_stdio(false);//加速
    cin >> M;
    ans[0] = 1;
    for (int i=1;i<=100;++i) ans[i] = ans[i-1]*3%mod;
    cout << ans[M] << endl;
}

猜你喜欢

转载自blog.csdn.net/Sherry_Yue/article/details/87391413