快速幂+快速乘

Given abc find the value of ab mod c (1 ≤ abc < 263).

Input

Contains multiple test cases. Each test is given in one line and contains three integers ab and c.

Output

For each test case print on a separate line the value of ab mod c.

Example 1

Input example #1
3 2 4
2 10 1000
Output example #1
1
24
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<cmath>
#include<string>
#include<map>
#include<queue>
using namespace std;
typedef unsigned long long ll;
ll a;
ll b,c;

ll mm(ll a,ll b){
	ll sum=0;
	while(b){
		if(b&1){
			sum=(sum+a)%c;
		}
		b>>=1;
		a=(a+a)%c;
	}
	return sum;
}

int main(){
	while(~scanf("%lld %lld %lld",&a,&b,&c)){
		ll sum=1;
		a=(ll)a%c;
		while(b>0){
			if(b&1)
			sum=(mm(sum,a))%c;
			b>>=1;
			a=(mm(a,a))%c;
		}
		printf("%lld\n",sum);
	}
	return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/akpower/p/11325728.html
今日推荐