#include <cstdio>
#include <vector>
#include <iostream>
using namespace std;
typedef long long ll;
ll sqr(ll v)
{
return v*v;
}
ll bigMod(ll p, ll v, ll m)
{
if (p==0) return 1;
else if (p&1) return (v%m * bigMod(p-1,v,m))%m;
else return (sqr(bigMod(p/2,v,m)))%m;
}
int main()
{
int tst;
ll x, y, n;
cin >> tst;
while (tst--)
{
cin >> x >> y >> n;
cout << bigMod(y,x,n) << endl;
}
cin >> tst;
return 0;
}
Showing posts with label Big Mod. Show all posts
Showing posts with label Big Mod. Show all posts
Thursday, November 10, 2011
[UVa] 1230 - MODEX
Sunday, October 23, 2011
[UVa] 10190 - Divide but not quite conquer
Critical point:
If n<2 or m<2, it's "Boring!"
If at some point (n%m != 0), it's "Boring!" to the limit :P
Method:
I store the result in a string (char arrray) when the division is taking place. If the sequence is found to be not boring, print the string.
If n<2 or m<2, it's "Boring!"
If at some point (n%m != 0), it's "Boring!" to the limit :P
Method:
I store the result in a string (char arrray) when the division is taking place. If the sequence is found to be not boring, print the string.
#include #include <stdio.h>
#include <string.h>
char output[1000000], temp[1000000];
int addStr(char *des, int val, int len)
{
if (!len)
sprintf(des,"%d",val);
else
{
int i, j;
sprintf(temp," %d",val);
int ln = strlen(temp);
for (i=len, j=0 ; j<ln ; i++, j++)
{
des[i]=temp[j];
}
des[i]='\0';
return (len+j);
}
return strlen(des);
}
int main()
{
int n, m;
while (scanf("%d %d",&n,&m)==2)
{
int lnt = 0;
int b=0;
if (n<2 || m<2)
{
printf("Boring!\n");
continue;
}
while (n>1)
{
lnt=addStr(output,n,lnt);
if (n%m != 0)
b=1;
n/=m;
}
lnt=addStr(output,n,lnt);
if (b)
printf("Boring!\n");
else
printf("%s\n",output);
}
return 0;
}
Saturday, October 22, 2011
[UVa] 374 - Big Mod
Used a recursive method from Art of Programming Contest
LL bigMod(LL b, LL p, LL m)
{
if (!p) return 1;
else if (!(p&0x01)) return power(bigMod(b,p/2,m),2)%m;
else return (b%m * bigMod(b,p-1,m))%m;
}
#include <stdio.h>
#define LL long
LL power(LL a, LL p)
{
int ret=1;
while (p--)
ret*=a;
return ret;
}
LL bigMod(LL b, LL p, LL m)
{
if (!p) return 1;
else if (!(p&0x01)) return power(bigMod(b,p/2,m),2)%m;
else return (b%m * bigMod(b,p-1,m))%m;
}
int main()
{
LL b, p, m;
while (scanf("%ld %ld %ld",&b,&p,&m)==3)
{
printf("%ld\n",bigMod(b,p,m));
}
return 0;
}
Subscribe to:
Posts (Atom)
Connect Rapoo MT750S with Linux (Tested on Manjaro)
I bought this obvious copy of MX Master 2S in hopes of having the device switching functionality along with a lightweight body because I ha...
-
I like coding a lot, keeps me glued to the PC for hours. For that reason it's a need to edit the Syntax Highlighter to suit my eyes for...
-
I've been constantly switching between my two OSs, Win7 and Linux Mint for just flashing a ROM to my Xperia device. And ofcourse, I hat...
-
Method: The problem at first glance seems too straightforward but it's not that much. Think a bit about the lines "Erin can add ...