poj 3090 (欧拉函数,找规律)

poj 3090 (欧拉函数,找规律)

题目:

给出一个n*n的点阵,求从(0,0)出发斜率不相等的直线有多少条。

限制:

1 <= n <= 1000

思路:

先定义sum[i]

sum[i] = 0, if(i == 1)

sum[i] = sum[i-1] + phi[i], if(i >= 2)

ans = sum[n] * 2 + 3

/*poj 3090
题目:
给出一个n*n的点阵,求从(0,0)出发斜率不相等的直线有多少条。
限制:
1 <= n <= 1000
思路:
先定义sum[i]
sum[i] = 0, if(i == 1)
sum[i] = sum[i-1] + phi[i], if(i >= 2)
ans = sum[n] * 2 + 3
*/
#include<iostream>
#include<cstdio>
using namespace std;
const int N=1005;
int pri[N],pcnt;
int phi[N];
void getphi(){
phi[1]=1;
for(int i=2;i<N;++i){
if(!phi[i]){ pri[pcnt++]=i; phi[i]=i-1; }
for(int j=0;i*pri[j]<N && j<pcnt;++j){
if(i%pri[j]==0){
phi[i*pri[j]]=phi[i]*pri[j];
break;
}
else phi[i*pri[j]]=phi[i]*(pri[j]-1);
}
}
}
int sum[N];
int main(){
int n;
getphi();
sum[2]=phi[2];
for(int i=3;i<N;++i)
sum[i]=sum[i-1]+phi[i];
int T,cas=0;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
printf("%d %d ",++cas,n);
printf("%d\n",sum[n]*2+3);
}
return 0;
}

时间: 2024-08-26 09:47:03

poj 3090 (欧拉函数,找规律)的相关文章

POJ 3090 欧拉函数

求一个平面内可见的点,其实就是坐标互质即可,很容易看出来或者证明 所以求对应的欧拉函数即可 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int phi[1010]; int n; void calc(int x) { for (int i=2;i<=x;i++) phi[i]=0; phi[1]=1;

hdu-5597 GTW likes function(欧拉函数+找规律)

题目链接: GTW likes function Time Limit: 4000/2000 MS (Java/Others)     Memory Limit: 131072/131072 K (Java/Others) Problem Description Now you are given two definitions as follows. f(x)=∑xk=0(−1)k22x−2kCk2x−k+1,f0(x)=f(x),fn(x)=f(fn−1(x))(n≥1) Note that

POJ 3090 (欧拉函数) Visible Lattice Points

题意: UVa 10820 这两个题是同一道题目,只是公式有点区别. 给出范围为(0, 0)到(n, n)的整点,你站在原点处,问有多少个整点可见. 对于点(x, y), 若g = gcd(x, y) > 1,则该点必被点(x/g, y/g)所挡住. 因此所见点除了(1, 0)和(0, 1)满足横纵坐标互素. 最终答案为,其中的+3对应(1, 1) (1, 0) (0, 1)三个点 1 #include <cstdio> 2 3 const int maxn = 1000; 4 int

poj 2480 欧拉函数+积性函数+GCD

题目:http://poj.org/problem?id=2480 首先要会欧拉函数:先贴欧拉函数的模板,来源于吉林大学的模板: //欧拉函数PHI(n)表示的是比n小,并且与n互质的正整数的个数(包括1). unsigned euler(unsignedx) {// 就是公式 unsigned i, res=x; for(i = 2; i < (int)sqrt(x * 1.0) + 1; i++) if(x%i==0) { res = res / i * (i - 1); while(x %

POJ 2478 欧拉函数打表的运用

http://poj.org/problem?id=2478 此题只是用简单的欧拉函数求每一个数的互质数的值会超时,因为要求很多数据的欧拉函数值,所以选用欧拉函数打表法. PS:因为最后得到的结果会很大,所以结果数据类型不要用int,改为long long就没问题了 #include <iostream> #include <stdio.h> using namespace std; #define LL long long LL F[1000100]; int phi[10001

POJ 2478 欧拉函数(欧拉筛法) HDU 1576 逆元求法

相关逆元求法,我之前有写过,还有欧拉函数的求法,欧拉函数与逆元的关系  点击 POJ 2478 又是一个打表的题目,一眼看出结果就是前n个欧拉函数值的和. 这里直接计算欧拉函数值求和会超时,看见多组数据. 然后就是计算欧拉函数,打表就好了. #include <stdio.h> #include <string.h> #include <iostream> using namespace std; typedef long long LL; const int N =

Farey Sequence POJ - 2478 (欧拉函数 前缀和)

Farey Sequence POJ - 2478 题目链接:https://vjudge.net/problem/POJ-2478 题目: 法理序列Fn是指对于任意整数n( n >= 2),由不可约的分数a/b(0 < a < b <= n),gcd(a,b) = 1升序排列构成的序列,最开始的几个如下 F2 = {1/2} F3 = {1/3, 1/2, 2/3} F4 = {1/4, 1/3, 1/2, 2/3, 3/4} F5 = {1/5, 1/4, 1/3, 2/5,

poj 3696 欧拉函数

poj 3696 题意: 给出一个数字L,求出最短的888...8能被L整除,输出最短的长度. 限制: 1 <= L <= 2*10^9 思路: 设x为最小长度 888...8=(10^x-1)/9*8 由题意得: (10^x-1)/9*8 % L=0 -> (10^x-1)*8 % (9L) = 0 -> (10^x-1) % (9L/gcd(L,8)) = 0 -> 10^x % (9L/gcd(L,8)) = 1 这个是一个离散对数的问题,第一个想到的是用拓展BSGS做

A Simple Stone Game-找素因子(欧拉函数)-2017中国大学生程序设计竞赛-哈尔滨站-重现赛

A Simple Stone Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 0    Accepted Submission(s): 0 Problem Description After he has learned how to play Nim game, Bob begins to try another ston