HDU - 4215 Number Theory? 打表

In number theory, for a positive number N, two properties are often mentioned, one is Euler's function, short for E(N), another is factor number, short for F(N).
To be more precise for newbie, here we recall the definition of E(N) and F(N) again.
E(N) = |{i | gcd(N, i) = 1, 1 <= i <= N}|
F(N) = |{i | N % i = 0, 1 <= i <= N}|
Here |Set| indicates the different elements in the Set.
As a number fanaticism, iSea want to solve a simple problem now. Given a integer N, try to find the number of intervals [l, r], l is no bigger than r obviously, strictly fit in the interval [1, N]. It's a piece of cake for clever you, of course. But here he also has another troublesome restrict:

Input

The first line contains a single integer T, indicating the number of test cases. Each test case includes one integer N.

Technical Specification
1. 1 <= T <= 1 000
2. 1 <= N <= 1 000 000 000

Output

For each test case, output the case number first, then the number of intervals.

Sample Input

2
2
9

Sample Output

Case 1: 1
Case 2: 6

题意:求有多少区间段[l,r](1<=l<=r<=n),满足上式。

题解:n达到了1e9,打表试一下,果然。。。。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=270000;
typedef long long ll;
int cnt1[N],cnt2[N];
int ans[N];
int main()
{
	for(int i=1;i<=30;i++)
	{
		int f1=0;
		int f2=0;
		for(int j=1;j<=i;j++)
		{
			if(__gcd(j,i)==1) f1++;
			if(i%j==0) f2++;
		}
		cnt1[i]=f1+cnt1[i-1];
		cnt2[i]=f2+cnt2[i-1];
	//	printf("%d %d\n",f1,f2);
	}
	for(int i=1;i<=30;i++)
	{
		int f=0;
		for(int j=1;j<=i;j++)
		{
		
			for(int k=j;k<=i;k++)
			{
				if(cnt1[k]-cnt1[j-1]==cnt2[k]-cnt2[j-1]) f++;
			}
		}
		ans[i]=f;
	//	printf("%d: %d\n",i,f);
	}
	int T,nn=1;
	int n;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		if(n>=30) printf("Case %d: 10\n",nn++);
		else printf("Case %d: %d\n",nn++,ans[n]);
	}
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/mmk27_word/article/details/85222673