本文主要是介绍牛客——薛定谔的签到(简单幂运算),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
最开始我是直接用pow函数做的,但是它一直报不能通过全部示例。想好久发现,如果f1和f2输入为0的话,那么就是有问题的,所以不能直接用pow函数。以下是这道题的不同做法:
#include<iostream>
#include<cmath>
#include<vector>
using namespace std;
long long num1(long long arr1,long long arr2,long long p)
{long long num=1;if(arr2!=0){for(;arr2>0;arr2--){num=num*arr1%p;}return num;}else return num;}
int main()
{vector<long long>arr;arr.clear();long long f1=0,f2=0,p=0,A=0,n=0;long long num=0;cin>>f1>>f2>>p>>A>>n;arr.push_back(f1);arr.push_back(f2);cout<<arr[0];if(n>=2) cout<<" "<<arr[1];for(long long x=3;x<=n;x++){num=floor(log2(num1(arr[x-2],arr[x-3],p)+1)+A);arr.push_back(num);cout<<" "<<arr[x-1];}return 0;
}
#include<bits/stdc++.h>
#include<math.h>
using namespace std;
int main()
{int x,y,p,a,n;cin>>x>>y>>p>>a>>n;int r[n];r[0]=x;r[1]=y;for(int i=2;i<n;i++){int t=1;for(int j=1;j<=r[i-2];j++){t*=r[i-1];t%=p;}r[i]=log2(t+1)+a;}for(int i=0;i<n;i++){cout<<r[i]<<" ";}return 0;
}
#include <bits/stdc++.h>
using namespace std;#define N 100005
#define LL long long int
#define PII pair<int,int>int p, a, n;
int f[105];int calcu(int f1, int f2) {int tmp = 1;for (int i = 0; i < f2; i++) { tmp *= f1;tmp %= p;}return log2(tmp + 1) + a;
}int main()
{cin >> f[1] >> f[2] >> p >> a >> n;if (n >= 1) cout << f[1] << " ";if (n >= 2) cout << f[2] << " ";for (int i = 3; i <= n; i++) {f[i] = calcu(f[i - 1], f[i - 2]);cout << f[i] << " ";}return 0;
}
#include <bits/stdc++.h>
#define int long long
using namespace std;int power(int a, int b,int MOD) {int ans = 1;while (b > 0) {if (b & 1)ans = (1ll * ans * a) % MOD;a = (1ll * a * a) % MOD;b >>= 1;}return ans;
}signed main() {ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);int f1, f2, MOD,A, n;cin >> f1 >> f2 >> MOD >> A >> n;vector<int> a(n);a[0] = f1;a[1] = f2;for (int i = 2; i < n; ++i) {int res = power(a[i - 1], a[i - 2],MOD);a[i] = (int)(log2(res + 1) + A);}for (int i = 0; i < n; ++i) {cout << a[i] << " ";}return 0;
}
本质上都是简单幂运算。
#include <bits/stdc++.h>#define int long longint MOD;
int power(int a, int b) {int ans = 1;while (b > 0) {if (b & 1)ans = 1ll * ans * a % MOD;a = 1ll * a * a % MOD;b >>= 1;}return ans;
}void solve() {int x, y, A, n;std::cin >> x >> y >> MOD >> A >> n;std::vector<int> a(n);a[0] = x;a[1] = y;for(int i = 2; i < n; ++i){a[i] = (int)std::log2(power(a[i - 1], a[i - 2]) + 1) + A;}for(int i = 0; i < n; ++i){std::cout << a[i] << " ";}
}signed main() {std::ios::sync_with_stdio(false);std::cin.tie(nullptr), std::cout.tie(nullptr);int _ = 1;//std::cin >> _;while (_--) {solve();}return 0;
}
这篇关于牛客——薛定谔的签到(简单幂运算)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!