每组测试数据包括一行,x(0<x<2000000000), y(0<y<2000000000), k(2<=k<=16)
输入可能有多组数据,对于每一组数据,root(x^y, k)的值
4 4 10
4
#include <stdio.h> int root(int N,int k) { if(N >= k) { int num = 0; while(N != 0) { num += N%k; N /= k; } return root(num,k); } else return N; } void get(int x,int y,int k) // root(x*y,k) = root(root(x,k)*root(y,k),k) { int tmp = root(x,k); int rtn = 1; while(y>0) { if(y%2 == 1) { rtn = root(rtn*tmp,k); } tmp = root(tmp*tmp,k); y/=2; } printf("%d\n",rtn); } int main() { int x,y,k; while(scanf("%d%d%d",&x,&y,&k) != EOF) { get(x,y,k); } }
#include <stdio.h> int main() { int x,y,k; while(scanf("%d%d%d",&x,&y,&k)!=EOF) { int z=1; while(y>=1) { x%=(k-1); if(y%2==1) { z*=x; z%=(k-1); } x*=x; y/=2; } if(z==0) { z=k-1; } printf("%d\n",z); } return 0; }
N'=a0+a1+a2+……+an;
N-N'=a1*(k-1)+a2*(k^2-1)+……+an*(k^n-1);
提取(k-1)有: (N-N')%(K-1)=0;
继续递推下去有: (N-N')%(k-1) =0;
try: while True: x,y,k = list(map(int,input().split())) result = 1 while y: if y&1: result = (result*x)%(k-1) x = (x*x)%(k-1) y>>=1 result = result if result else k-1 print(result) except Exception: pass
#include<stdio.h> int mod; long long FastPow(long long a,long long k){ long long sum=1; while(k){ if(k&1)sum=sum*a%mod; a=a*a%mod; k>>=1; } return sum; } int main(){ long long x,y; while(scanf("%lld%lld%d",&x,&y,&mod)!=EOF){ mod--; long long p=FastPow(x,y); printf("%lld\n",p==0?mod:p); } }
// 网上抄来的,来源:http://www.acmerblog.com/jiudu-1085-2256.html #include <stdio.h> #include <string.h> #include <stdlib.h> #include<iostream> using namespace std; typedef long long INT; //计算幂取模a^b mod n, O(logb) int modular_exponent(INT a,INT b,INT n){ int ret=1; for (;b;b>>=1,a=a*a%n) if (b&1) ret=ret*a%n; return ret; } int main() { int x, y, k; int i; int ret; while(scanf("%d%d%d", &x, &y, &k) == 3) { ret = modular_exponent(x, y, k-1); if(ret == 0) ret = k-1; printf("%d\n", ret); } return 0; }
//我用两种方法求这个题。它们思想都是快速幂加上递推式。 //下面是迭代 #include<stdio.h> typedef long long ll; //迭代求x^y%k ll N(ll x,ll y,ll k){ ll ans=1; while(y){ if(y&1) ans=(ans*x)%k; x=(x*x)%k; y>>=1; } if(ans)//模不为0返回模 return ans; else//模为0返回k return k; } int main(){ ll x,y,k; while(scanf("%lld%lld%lld",&x,&y,&k)!=EOF){//循环输入 printf("%lld",N(x,y,k-1)); } return 0; } //下面是递归 #include<iostream> using namespace std; long N(long long x,long long y,long long k){ long long ans=0; if(!y) return 1; else if(y&1) return x*N(x,y-1,k)%k; else{ ans=N(x,y/2,k); return ans*ans%k; } } int main(){ long long x,y,k,ans; while(cin>>x>>y>>k){ ans=N(x,y,k-1); if(ans) cout<<ans; else cout<<k-1; } return 0; }
#include <iostream> using namespace std; long long root(long long x, long long y, int k) { long long ans = 1; while (y != 0) { if (y % 2 == 1) ans = x * ans % k; y /= 2; x = x * x % k; } return ans; } int main() { int x, y, k; cin >> x >> y >> k; int ans = root(x, y, k - 1); if (ans == 0) ans = k - 1; cout << ans << endl; return 0; }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { // TODO 自动生成的方法存根 Scanner in = new Scanner(System.in); while (in.hasNext()) { long aa = in.nextLong(); long bb = in.nextLong(); long kk = in.nextLong(); //快速幂取模 long ans = 1; kk--; while (bb > 0) { if (bb%2==1) { ans = (ans*aa)%kk; } bb/=2; aa=(aa*aa)%kk; } System.out.println(ans==0?kk:ans); } } }
#include <iostream> using namespace std; int root(long long a,long long b,int mod){ int answer = 1; while(b>0){ if(b % 2==1) answer=answer*a%mod; b/=2; a=a*a%mod; } return answer>0?answer:mod; } int main() { long long x,y; int k; while(cin>>x>>y>>k){ cout<<root(x,y,k-1)<<endl; } }
#include <iostream> using namespace std; long long FastPow(long long a, long long k, const int mod){ if(k==0) return 1; if(k%2==1){ return FastPow(a, k-1, mod) * a % mod; } else { long long ans = FastPow(a, k/2, mod); return ans * ans % mod; } } int main() { int x, y, k; while(cin>>x>>y>>k){ k --; long long ans = FastPow(x, y, k); if(ans==0) cout<<k<<endl; else cout<<ans<<endl; } return 0; }
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner in = new Scanner(System.in); while(in.hasNext()){ long x = in.nextLong(); long y = in.nextLong(); long k = in.nextLong(); long res = root(x, y, k); System.out.println(res); } } //快速幂算法 public static long root(long x, long y, long k){ long res = 1; while(y > 0){ if(y % 2 == 1){ res = res * x % (k - 1); } x = x * x % (k - 1); y /= 2; } return res == 0 ? (k - 1) : res; } }
#include<iostream> #include<cstdio> using namespace std; long long root(long long x,long long y,int k) { long long ans = 1; while(y!=0) { if(y%2==1) ans = x*ans%k; y/=2; x = x*x%k; } return ans; } int main() { int x,y,k; cin>>x>>y>>k; int ans = root(x,y,k-1); if(ans == 0) ans = k-1; cout<<ans<<endl; return 0; }