列表

详情


NC23896. king

描述

国王有一块神奇土地
第一天可以产a吨粮食,第二天会变成前一天的a倍,以此类推。
n天后大臣准备把这些粮食尽可能多的平均分给b个城池
为了方便,每个城池分到的粮食都是整吨整吨哒!
剩下的粮食国王准备贪污
国王能贪到多少吨粮食呢?

输入描述

输入的第一行为一个数字T(T<=100),表示数据输入的组数。
之后每行3个数字,分别表示 a, n, b(1<=a,n<= 1000,000,000;1<=b<=1000 )。

输出描述

对于每组数据输出对应结果

示例1

输入:

1
2 2 3

输出:

1

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++11(clang++ 3.9) 解法, 执行用时: 4ms, 内存消耗: 336K, 提交时间: 2019-04-08 19:35:20

#include<bits/stdc++.h>
using namespace std;
int main()
{
	long long int t;
	cin>>t;
	while(t--)
	{
		long long int a,n,b,ans=1;
		cin>>a>>n>>b;
		while(n)
		{
			if(n&1)
			ans=(ans*a)%b;
			a=(a*a)%b;
			n/=2;
		}
		cout<<ans<<endl;
	}
}

C++14(g++5.4) 解法, 执行用时: 4ms, 内存消耗: 356K, 提交时间: 2019-04-03 19:54:43

#include<iostream>
using namespace std;
int main()
{
	long long int t,a,b,c;
	cin>>t;
	while(t--)
	{
		cin>>a>>b>>c;
		long long int ans=1;
		while(b)
		{
			if(b&1)
			ans=(ans*a)%c;
			a=(a*a)%c;
			b/=2;
		}
		cout<<ans<<endl;
	}
}

pypy3(pypy3.6.1) 解法, 执行用时: 37ms, 内存消耗: 18672K, 提交时间: 2020-09-13 17:49:16

for _ in range(int(input())):
    a,n,b = map(int,input().split())
    print(pow(a,n,b))

Python3(3.5.2) 解法, 执行用时: 17ms, 内存消耗: 3736K, 提交时间: 2020-08-25 14:43:03

for i in range(int(input())):
    a,n,b=map(int,input().split())
    print(pow(a,n,b))

上一题