HDU 5868 Different Circle Permutation

Description

You may not know this but it's a fact that Xinghai Square is Asia's largest city square. It is located in Dalian and, of course, a landmark of the city. It's an ideal place for outing any time of the year. And now: 
   
  There are   children from a nearby primary school flying kites with a teacher. When they have a rest at noon, part of them (maybe none) sit around the circle flower beds. The angle between any two of them relative to the center of the circle is always a multiple of   but always not 
   
  Now, the teacher raises a question: How many different ways there are to arrange students sitting around the flower beds according to the rule stated above. To simplify the problem, every student is seen as the same. And to make the answer looks not so great, the teacher adds another specification: two ways are considered the same if they coincide after rotating.

Input

There are   tests ( ). Each test contains one integer  . Process till the end of input.

Output

For each test, output the answer mod 1000000007 ( ) in one line.

Sample Input

4
7
10

Sample Output

3
5

15

数论水平尚低,搞不来推导。

搞到公式的话就简单了,直接照着打即可。

注意需要特判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 n;
int a[2], b[2][2], c[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 exgcd(int a, int b, int &x, int &y)
{
	if (!b) { x = 1; y = 0; return a; }
	int g = exgcd(b, a % b, x, y);
	int z = x - a / b * y;
	x = y;	y = z;	return g;
}

int get(int x)
{
	if (x == 1) return 1;
	a[0] = 0;	a[1] = 1;
	b[0][0] = b[0][1] = -1;
	b[1][0] = 1; b[1][1] = 2;
	for (--x; x; x >>= 1)
	{
		if (x & 1)
		{
			rep(i, 0, 1)
			{
				c[0][i] = 0;
				rep(j, 0, 1) c[0][i] = (1LL * a[j] * b[j][i] + c[0][i]) % mod;
			}
			rep(i, 0, 1) a[i] = c[0][i];
		}
		rep(i, 0, 1) rep(j, 0, 1)
		{
			c[i][j] = 0;
			rep(k, 0, 1) c[i][j] = (1LL * b[i][k] * b[k][j] + c[i][j]) % mod;
		}
		rep(i, 0, 1) rep(j, 0, 1) b[i][j] = c[i][j];
	}
	return ((a[0] + a[1]) % mod + mod) % mod;
}

int main()
{
	while (scanf("%d", &n) != EOF)
	{
		if (n == 1) { printf("2\n"); continue; }
		int x, y;	exgcd(n, mod, x, y);
		LL ans = 0;
		for (int i = 1; i*i <= n; i++)
		{
			if (n % i) continue;
			ans += 1LL * phi(n / i) * get(i) % mod;
			if (i*i == n) continue;
			ans += 1LL * phi(i) * get(n / i) % mod;
		}
		printf("%lld\n", ans % mod * (x % mod + mod) % mod);
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值