B. Fedya and Maths
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Fedya studies in a gymnasium. Fedya's maths hometask is to calculate the following expression:
for given value of n. Fedya managed to complete the task. Can you? Note that given number n can be extremely large (e.g. it can exceed any integer type of your programming language).
Input
The single line contains a single integer n (0 ≤ n ≤ 10105). The number doesn't contain any leading zeroes.
Output
Print the value of the expression without leading zeros.
Examples
input
4
output
4
input
124356983594583453458888889
output
0
Note
Operation x mod y means taking remainder after division x by y.
Note to the first sample:
题目不难,就是大数模4是否等于0,不过看官方题解的一种做法挺不错的,直接用类似快速幂的方法求大数的幂,学习一下。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <stack>
#include <bitset>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#define FOP freopen("data.txt","r",stdin)
#define FOP2 freopen("data1.txt","w",stdout)
#define inf_LL 4223372036854775807
#define inf 0x3f3f3f3f
#define maxn 1000010
#define mod 1000000007
#define PI acos(-1.0)
#define LL long long
using namespace std;
const int P = 5;
char s[maxn];
int a[maxn], n;
void get_num(char *s)
{
n = 0;
int len = strlen(s);
int i;
for(i = len - 3; i >= 0; i -= 3)
{
a[n++] = (s[i] - '0') * 100 + (s[i+1] - '0') * 10 + s[i+2] - '0';
}
i += 2;
for(int j = 0; j <= i; j++)
{
a[n] = a[n] * 10 + s[j] - '0';
if(j == i) n++;
}
}
int powmod(int x, int n, int P)
{
x %= P;
int res = 1 % P;
while(n)
{
if (n & 1)
{
res = (res * x) % P;
n--;
}
else
{
x = (x * x) % P;
n >>= 1;
}
}
return res;
}
int myPow(int x, int *a, int P)
{
x %= P;
int res = 1 % P;
int now = x;
for (int i = 0; i < n; i++)
{
res = (res * powmod(now, a[i], P)) % P;
now = powmod(now, 1000, P);
}
return res;
}
int main()
{
scanf("%s", s);
get_num(s);
int res = myPow(1, a, P) + myPow(2, a, P) + myPow(3, a, P) + myPow(4, a, P);
res %= P;
printf("%d\n", res);
return 0;
}