第三道题是二进制运算 与有关快速幂
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
void solve()
{
//记得开long long 此处宏定义ll为long long
ll k;cin>>k;
ll ans=0;
ll weight=1;
while(k){
if(k&1){//与1,能够判断最后一位是不是1
ans+=weight;
}
k>>=1;//然后位运算右移
weight*=3;
}
cout<<ans<<endl;
}
int main()
{
//关闭输入输出流同步
ios::sync_with_stdio(false);
cin.tie(nullptr);
//测试样例
ll t;
cin>>t;
while(t--)
{
solve();
}
}
附快速幂的模板
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
//快速幂模板,二进制运算
long long fastPower(long long base, long long power) {
long long result = 1;
while (power > 0) {
if (power & 1) {//此处等价于if(power%2==1)
result = result * base % 1000;
}
power >>= 1;//此处等价于power=power/2
base = (base * base) % 1000;
}
return result;
}
void solve(){
int zs,ds;
cin>>ds;
cin>>zs;
cout<<fastPower(ds,zs)<<endl;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin>>t;
while(t--){
solve();
}
}