HDU 4335 What is N?

Description

  (This problem is really old&easy and any acmer who is good at math may solve it in second) 
  As we know, math is both intangible and specific. Today you are going to solve the following math problem: 
  Given one non-negative integer b, one positive number P and one positive integer M, how many n could you find to satisfy the following 2 rules? 

  Here n! means n factorial , 0!=1,1!=1,2!=2,3!=6…,n!=(n-1)!*n

Input

  In the first line of the input , we have one integer T indicating the number of test cases. (1 <= T <= 20) 
  Then T cases follows. 
  For every case, only 3 integers (b, P and M) in a single line. ( 0<=b<P, 1<=P<=10^5, 1 <= M <=2^64 �C 1 )

Output

  For each test case, first you should print "Case #x: " in a line, where x stands for the case number started with 1. Then one integer indicates the number of "n" that you can find.

Sample Input

3
1 2 3
2 3 4
3 4 5

Sample Output

Case #1: 2
Case #2: 0

Case #3: 0

同样是欧拉函数降次,当阶乘大于phi(P)时,之后的指数都是phi(P),所以按照对p取模的余数分类即可。

需要注意一个坑点,当P=1且b=0且M=2^64-1的时候会溢出。。。

#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, n, m, cas = 0;
LL M;

int phi(int x)
{
	int res = 1;
	for (int i = 2; i*i <= x; i++)
	{
		if (x%i) continue;
		res *= i - 1;
		for (x /= i; !(x%i); 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;
}

int main()
{
	inone(T);
	while (T--)
	{
		intwo(n, m);	scanf("%llu", &M);
		if (m == 1)
		{
			if (M == 18446744073709551615) printf("Case #%d: 18446744073709551616\n", ++cas);
			else printf("Case #%d: %llu\n", ++cas, M + 1); continue;
		}
		int mod = phi(m), g = 1, f = 0;
		LL ans = (!n)*(M / m + 1);
		rep(i, 1, min(M, (LL)m - 1))
		{
			if (1LL * g * i >= mod) f = 1;
			g = 1LL * g * i % mod;
			ans += get(i, g + f * mod, m) == n;
			ans += (get(i, mod, m) == n) * (M / m + (M % m >= i) - 1);
		}
		printf("Case #%d: %llu\n", ++cas, ans);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值