hdu 6434 ( Problem I. Count )

Problem I. Count

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 418    Accepted Submission(s): 216

Problem Description

Multiple query, for each n, you need to get
n i-1
∑ ∑ [gcd(i + j, i - j) = 1]
i=1 j=1

Input

On the first line, there is a positive integer T, which describe the number of queries. Next there are T lines, each line give a positive integer n, as mentioned above.
T<=1e5, n<=2e7

Output

Your output should include T lines, for each line, output the answer for the corre- sponding n.

Sample Input

4
978
438
233
666

Sample Output

194041
38951
11065
89963

令a=i-j,所以i+j=2*i-a,那么原式可以化简为:

a一定是奇数。

1 欧拉函数的几个性质:f(n) :欧拉函数值
2 1.若a为质数,phi[a]=a-1;
3 2.若a为质数,b mod a=0,phi[a*b]=phi[b]*a
4 3.若a,b互质,phi[a*b]=phi[a]*phi[b](当a为质数时,if b mod a!=0 ,phi[a*b]=phi[a]*phi[b])
5 4.gcd(kx,ky)=gcd(x,y)==1
6 5.gcd(kx-y,y)=gcd(kx,y)=gcd(x,y)==1
7 6.若N为奇数,那么f(2*n)=2*f(n)
8 7.若n是质数p的k次幂,f(n)=p^k-p^(k-1)=(p-1)*p^(k-1)
9 因为除了p的倍数外,其他数都跟n互质。:1~n 每隔p个出现一个p的倍数
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<vector>
 7 #define N 20000009
 8 using namespace std;
 9 #define ll long long
10 int pre[N],phi[N];
11 ll sum[N];
12 bool vis[N];
13 int t,n;
14 //欧拉函数 :phi[i] j:(1~i)里有phi[i]个数令gcd(i,j)=1
15 void init()
16 {
17     pre[1]=1;
18     int i,j;
19     int pree=0;
20     for(i=2;i<=N;i++)
21     {
22         if(!vis[i]){
23             pre[++pree]=i;
24             phi[i]=i-1;
25         }
26         for(j=1;j<=pree&&i*pre[j]<=N;j++){
27             vis[i*pre[j]]=1;
28             if(i%pre[j]==0){
29                 phi[i*pre[j]]=phi[i]*pre[j];
30                 break;
31             }
32             else{
33                 phi[i*pre[j]]=phi[i]*phi[pre[j]];
34             }
35         }
36     }
37     for(int i=1;i<=N;i++){
38         if(i&1) phi[i]>>=1;
39         sum[i]=sum[i-1]+phi[i];//求个前缀和即可
40         //if(i&1) phi[i]>>=1;
41         //因为a一定是奇数,那么偶数不变,因为偶数与偶数的gcd一定!=1
42         //奇数减半 如 7 : 1 2 3 4 5 6  而只有1 3 5 有效
43     }
44 }
45 int main()
46 {
47     init();
48     scanf("%d",&t);
49     while(t--){
50         scanf("%d",&n);
51         printf("%lld\n",sum[n]);
52     }
53     return 0;
54 }

原文地址:https://www.cnblogs.com/tingtin/p/9522956.html

时间: 2024-11-12 22:56:45

hdu 6434 ( Problem I. Count )的相关文章

HDU 4910 Problem about GCD

Problem about GCD Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 470    Accepted Submission(s): 77 Problem Description Given integer m. Find multiplication of all 1<=a<=m such gcd(a, m)=1 (cop

HDU 2256 Problem of Precision (矩阵快速幂)

HDU 2256 Problem of Precision (矩阵快速幂) ACM 题目地址:HDU 2256 Problem of Precision 题意: 给出一个式子,求值. 分析: 推起来最后那步会比较难想. 具体过程见: 表示共轭只听说过复数的和图的... 这构题痕迹好明显... 跟基友开玩笑说:如果遇到这种题,推到Xn+Yn*sqrt(6)这步时,打表最多只能打到10就爆int了,这是输出正解和Xn,说不定眼神好能发现ans = Xn * 2 - 1呢.= =... 代码: /*

hdu 4910 Problem about GCD(数论)

题目连接:hdu 4910 Problem about GCD 题目大意:给定M,判断所有小于M并且和M互质的数的积取模M的值. 解题思路:有个数论的结论,若为偶数,M=M/2. 可以写成M=pk,即只有一种质因子时,答案为M-1,否则为1.特殊情况为4的倍数,不包括4. 首先用1e6以内的素数去试除,如果都不可以为p,那么对大于1e6的情况判断一下是否为素数,是素数也可以(k=1),否则开方计算,因为M最大为1e18,不可能包含3个大于1e6的质因子. #include <cstdio> #

HDU 4910 Problem about GCD(米勒拉宾)

HDU 4910 Problem about GCD 题目链接 题意:给定一个数字,求出1 - n之间与他互质的数的乘积mod n 思路:看了网上别人找出来的规律,原文链接 然后由于这题的n很大,也没法直接判定,可以这样搞,先去试10^6以内的素数,判断可不可以,如果不行,再利用米勒拉宾判下是否是素数,如果不是的话,把这个数字开根在平方,判断是不是完全平方数,这样做的原因是数字最大10^18,如果没有10^6以内的质因子,又不是质数的话,那么他最多只能包含2个质因子了,那么如果他不是一个完全平方

hdu String Problem(最小表示法入门题)

hdu 3374 String Problem 最小表示法 view code#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <map> #include <string> using namespace std; const int N = 10010; int n; char s[105]; map<

HDU Train Problem II (卡特兰数+大数)

Problem Description As we all know the Train Problem I, the boss of the Ignatius Train Station want to know if all the trains come in strict-increasing order, how many orders that all the trains can get out of the railway.   Input The input contains

hdu 5685 Problem A(2016&quot;百度之星&quot; - 资格赛(Astar Round1)——线段树)

题目链接:acm.hdu.edu.cn/showproblem.php?pid=5685 Problem A Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 564    Accepted Submission(s): 236 Problem Description 度熊手上有一本字典存储了大量的单词,有一次,他把所有单词组成了一个很长

[Daily Coding Problem 68] Count Pairs of attacking bishop pairs

This problem was asked by Google. On our special chessboard, two bishops attack each other if they share the same diagonal. This includes bishops that have another bishop located between them, i.e. bishops can attack through pieces. You are given N b

HDU 5688 Problem D map

Problem D Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 394    Accepted Submission(s): 273 Problem Description 度熊所居住的 D 国,是一个完全尊重人权的国度.以至于这个国家的所有人命名自己的名字都非常奇怪.一个人的名字由若干个字符组成,同样的,这些字符的全排列的结果中的每