HDU 3221 Brute-force Algorithm
2016-09-29 17:28
288 查看
Description
Professor Brute is not good at algorithm design. Once he was asked to solve a path finding problem. He worked on it for several days and finally came up with the following algorithm:
Any fool but Brute knows that the function “funny” will be called too many times. Brute wants to investigate the number of times the function will be called, but he is too lazy to do it.
Now your task is to calculate how many times the function “funny” will be called, for the given a, b and n. Because the answer may be too large, you should output the answer module by P.
Input
There are multiple test cases. The first line of the input contains an integer T, meaning the number of the test cases.
For each test cases, there are four integers a, b, P and n in a single line.
You can assume that 1≤n≤1000000000, 1≤P≤1000000, 0≤a, b<1000000.
Output
For each test case, output the answer with case number in a single line.
Sample Input
Sample Output
先推出公式,答案就是a^fib(n-2)*b^fib(n-1)%p,然后根据欧拉定理降次即可。
#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
#define loop(i,j,k) for (int i = j;i != -1; i = k[i])
#define lson x << 1, l, mid
#define rson x << 1 | 1, mid + 1, r
#define ff first
#define ss second
#define mp(i,j) make_pair(i,j)
#define pb push_back
#define pii pair<int,LL>
#define inone(x) scanf("%d", &x);
#define intwo(x,y) scanf("%d%d", &x, &y);
using namespace std;
typedef unsigned long long LL;
const int low(int x) { return x&-x; }
const double eps = 1e-4;
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int N = 1e5 + 10;
int T, a, b, p, n, cas = 0, f[32];
int c[2], d[2][2], e[2][2];
int phi(int x)
{
int res = 1;
for (int i = 2; i*i <= x; i++)
{
if (x % i) continue;
res *= i - 1; x /= i;
while (x%i == 0) res *= i, x /= i;
}
return res * max(x - 1, 1);
}
int get(int x, int y, int z)
{
int res = 1;
for (; y; y >>= 1)
{
if (y & 1) res = 1LL * res*x%z;
x = 1LL * x*x%z;
}
return res%z;
}
int main()
{
f[0] = 0; f[1] = 1;
rep(i, 2, 31) f[i] = f[i - 1] + f[i - 2];
inone(T);
while (T--)
{
scanf("%d%d%d%d", &a, &b, &p, &n);
if (n == 1) { printf("Case #%d: %d\n", ++cas, a%p); continue; }
if (n == 2) { printf("Case #%d: %d\n", ++cas, b%p); continue; }
if (n <= 32)
{
printf("Case #%d: %d\n", ++cas, 1LL * get(a, f[n - 2], p)*get(b, f[n - 1], p) % p);
}
else
{
int mod = phi(p);
c[0] = 0; c[1] = 1;
d[0][0] = 0; d[1][0] = 1;
d[0][1] = 1; d[1][1] = 1;
for (int x = n - 2; x; x >>= 1)
{
if (x & 1)
{
rep(i, 0, 1)
{
e[0][i] = 0;
rep(j, 0, 1) e[0][i] += 1LL * c[j] * d[j][i] % mod;
}
rep(i, 0, 1) c[i] = e[0][i] % mod;
}
rep(i, 0, 1) rep(j, 0, 1)
{
e[i][j] = 0;
rep(k, 0, 1) e[i][j] += 1LL * d[i][k] * d[k][j] % mod;
}
rep(i, 0, 1) rep(j, 0, 1) d[i][j] = e[i][j] % mod;
}
printf("Case #%d: %d\n", ++cas, 1LL * get(a, c[0] + mod, p)*get(b, c[1] + mod, p) % p);
}
}
return 0;
}
Professor Brute is not good at algorithm design. Once he was asked to solve a path finding problem. He worked on it for several days and finally came up with the following algorithm:
Any fool but Brute knows that the function “funny” will be called too many times. Brute wants to investigate the number of times the function will be called, but he is too lazy to do it.
Now your task is to calculate how many times the function “funny” will be called, for the given a, b and n. Because the answer may be too large, you should output the answer module by P.
Input
There are multiple test cases. The first line of the input contains an integer T, meaning the number of the test cases.
For each test cases, there are four integers a, b, P and n in a single line.
You can assume that 1≤n≤1000000000, 1≤P≤1000000, 0≤a, b<1000000.
Output
For each test case, output the answer with case number in a single line.
Sample Input
3 3 4 10 3 4 5 13 5 3 2 19 100
Sample Output
Case #1: 2 Case #2: 11Case #3: 12
先推出公式,答案就是a^fib(n-2)*b^fib(n-1)%p,然后根据欧拉定理降次即可。
#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
#define loop(i,j,k) for (int i = j;i != -1; i = k[i])
#define lson x << 1, l, mid
#define rson x << 1 | 1, mid + 1, r
#define ff first
#define ss second
#define mp(i,j) make_pair(i,j)
#define pb push_back
#define pii pair<int,LL>
#define inone(x) scanf("%d", &x);
#define intwo(x,y) scanf("%d%d", &x, &y);
using namespace std;
typedef unsigned long long LL;
const int low(int x) { return x&-x; }
const double eps = 1e-4;
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int N = 1e5 + 10;
int T, a, b, p, n, cas = 0, f[32];
int c[2], d[2][2], e[2][2];
int phi(int x)
{
int res = 1;
for (int i = 2; i*i <= x; i++)
{
if (x % i) continue;
res *= i - 1; x /= i;
while (x%i == 0) res *= i, x /= i;
}
return res * max(x - 1, 1);
}
int get(int x, int y, int z)
{
int res = 1;
for (; y; y >>= 1)
{
if (y & 1) res = 1LL * res*x%z;
x = 1LL * x*x%z;
}
return res%z;
}
int main()
{
f[0] = 0; f[1] = 1;
rep(i, 2, 31) f[i] = f[i - 1] + f[i - 2];
inone(T);
while (T--)
{
scanf("%d%d%d%d", &a, &b, &p, &n);
if (n == 1) { printf("Case #%d: %d\n", ++cas, a%p); continue; }
if (n == 2) { printf("Case #%d: %d\n", ++cas, b%p); continue; }
if (n <= 32)
{
printf("Case #%d: %d\n", ++cas, 1LL * get(a, f[n - 2], p)*get(b, f[n - 1], p) % p);
}
else
{
int mod = phi(p);
c[0] = 0; c[1] = 1;
d[0][0] = 0; d[1][0] = 1;
d[0][1] = 1; d[1][1] = 1;
for (int x = n - 2; x; x >>= 1)
{
if (x & 1)
{
rep(i, 0, 1)
{
e[0][i] = 0;
rep(j, 0, 1) e[0][i] += 1LL * c[j] * d[j][i] % mod;
}
rep(i, 0, 1) c[i] = e[0][i] % mod;
}
rep(i, 0, 1) rep(j, 0, 1)
{
e[i][j] = 0;
rep(k, 0, 1) e[i][j] += 1LL * d[i][k] * d[k][j] % mod;
}
rep(i, 0, 1) rep(j, 0, 1) d[i][j] = e[i][j] % mod;
}
printf("Case #%d: %d\n", ++cas, 1LL * get(a, c[0] + mod, p)*get(b, c[1] + mod, p) % p);
}
}
return 0;
}
相关文章推荐
- HDU 3220 Alice’s Cube:题目解答源码
- 【HDU 5366】The mook jong 详解
- 【HDU 2136】Largest prime factor 详细图解
- 【HDU 1568】Fibonacci 数学公式 详解
- HDU 1568
- HDU1290
- HDU1568(Fobonacci公式)
- HDU ACM Step 2.2.2 Joseph(约瑟夫环问题)
- HDU 1405
- HDU 1297
- hdu 1205
- hdu 2087
- hdu 1016
- HDU 4898 The Revenge of the Princess’ Knight ( 2014 Multi-University Training Contest 4 )
- HDU 5592 ZYB's Premutation 线段树(查找动态区间第K大)
- HDU 5240 Exam (好水的题)
- HDU5237 Base64 大模拟
- HDU 1000
- HDU 1001
- HDU 1016 Prime Ring Problem