题解分享
题解分享简介
八次求和(编程题) - 题解
```
import java.util.Scanner;
public class Main {
// 主函数
public static void main(String[] args) {
// 读取输入的整数
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
// 计算结果
long result = 0;
for (int i = 1; i <= n; i++) {
result += pow(i, 8); // 调用自定义幂函数并累加结果
result %= 123456789; // 取模操作
}
// 输出结果
System.out.println(result); // 打印最终结果
}
// 自定义幂函数
public static long pow(long base, long exponent) {
long result = 1;
while (exponent > 0) {
if (exponent % 2 == 1) {
result = (result * base) % 123456789; // 计算幂的过程中取模
}
base = (base * base) % 123456789; // 计算幂的过程中取模
exponent >>= 1; // 右移一位,相当于除以2
}
return result;
}
}
```
查看全文
0
0
0
7
八次求和(编程题) - 题解
```
#include <bits/stdc++.h>
#define objl '\n'
typedef long long ll;
using namespace std;
//数据结构在这里定义
ll n;
ll m = 123456789;
ll bpm(ll b, ll p, ll m){
ll res = 1 % m;
while(p!=0){
if(p&1){
res = (res % m) * (b % m) % m;
}
b = (b % m) * (b % m) % m;
p = p >> 1;
}
return res;
}
void solve(){
//cin cout在这里
cin >> n;
ll ans = 0;
for(int x = 1; x<= n; x++){
ans = ((ans % m) + (bpm(x,8,m) % m)) % m;
}
cout << ans << endl;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;//多组数据要cin
//cin >> t;
t = 1;
while(t--){
solve();
}
return 0;//必须加return 0
}
```
查看全文
0
0
0
1
八次求和(编程题) - 题解
```cpp
#include <bits/stdc++.h>
using namespace std;
#define int long long
// #define LL long long
#define endl '\n'
const int MOD = 123456789;
int n;
int quick_power(int a, int n)
{
a = a % MOD;
int r = 1;
while (n != 0)
{
if (n % 2 == 1)
r = r * a % MOD;
a = (a * a) % MOD;
n = n / 2;
}
return r;
}
void solve()
{
cin >> n;
int sum = 0;
for (int i = 1; i <= n; i++)
{
sum += quick_power(i, 8);
}
cout << sum % MOD;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
solve();
return 0;
}
```
查看全文
0
0
0
0
八次求和(编程题) - 题解
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
```
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
BigInteger bi = new BigInteger("0");
for (int i = 0; i <= n; i++) {
BigInteger temp = new BigInteger(i+"");
temp = temp.pow(8);
bi = bi.add(temp);
bi = bi.mod(new BigInteger("123456789"));
}
System.out.println(bi.toString());
}
```
}
查看全文
0
0
0
0
八次求和(编程题) - 题解
```C++
#include <iostream>
using namespace std;
using ll = long long;
ll mod = 123456789;
ll myPow(ll a, ll b) {
ll res = 1;
while (b) {
if (b & 1)
res = res * a % mod;
b >>= 1;
a = a * a % mod;
}
return res;
}
int main() {
int n;
cin >> n;
ll res = 0;
for (int i = 1; i <= n; ++i) {
res = (res + myPow(i, 8)) % mod;
}
cout << res << '\n';
return 0;
}
```
查看全文
0
0
0
0



