HDU 5597 GTW likes function(规律+欧拉函数模板题)——BestCoder Round #66(div.1 div.2)

GTW likes function

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0


Problem Description
Now you are given two definitions as follows.

f(x)=xk=0(1)k22x2kCk2xk+1,f0(x)=f(x),fn(x)=f(fn1(x))(n1)

Note that  φ(n)  means Euler’s totient function.( φ(n)   is an arithmetic function that counts the positive integers less than or equal to n that are relatively prime to n.)

For each test case, GTW has two positive integers —  n  and  x , and he wants to know the value of the function  φ(fn(x)) .
 

Input
There is more than one case in the input file. The number of test cases is no more than 100. Process to the end of the file.

Each line of the input file indicates a test case, containing two integers,  n  and  x , whose meanings are given above.  (1n,x1012)
 

Output
In each line of the output file, there should be exactly one number, indicating the value of the function  φ(fn(x))    of the test case respectively.
 

Sample Input
   
   
   
   
1 1 2 1 3 2
 

Sample Output
   
   
   
   
2 2 2
 

Source
BestCoder Round #66 (div.2)
 

/************************************************************************/

附上该题对应的中文题

GTW likes function

 
 
 Time Limit: 4000/2000 MS (Java/Others)
 
 Memory Limit: 131072/131072 K (Java/Others)
问题描述
现在给出下列两个定义:
	f(x)=f_{0}(x)=\sum_{k=0}^{x}(-1)^{k}2^{2x-2k}C_{2x-k+1}^{k},f_{n}(x)=f(f_{n-1}(x))(n\geq 1)f(x)=f0(x)=k=0x(1)k22x2kC2xk+1k,fn(x)=f(fn1(x))(n1)

	\varphi(n)φ(n)为欧拉函数。指的是不超过nn的与nn互质的正整数个数。

对于每组数据,GTW有两个正整数n,xn,x,现在他想知道函数\varphi(f_{n}(x))φ(fn(x))的值。
输入描述
输入有多组数据,不超过100组。每数据输入一行包含2个整数组nnxx(1\leq n,x \leq 10^{12})(1n,x1012)
输出描述
对于每组数据输出一行,表示函数\varphi(f_{n}(x))φ(fn(x))的值。
输入样例
1 1
2 1
3 2
输出样例
2
2
2
/****************************************************/

出题人的解题思路:

GTW likes function

由打表找规律可得,\sum_{k=0}^{x}(-1)^{k}2^{2x-2k}C_{2x-k+1}^{k}=x+1k=0x(1)k22x2kC2xk+1k=x+1,所以显然f_n(x)=n+x+1fn(x)=n+x+1,因此直接求\varphi(n+x+1)φ(n+x+1)。时间效率O(T\sqrt{n})O(Tn)

严格证明:

a_n=\sum_{k=0}^{n}(-1)^k2^{2n-2k}C_{2n-k+1}^kan=k=0n(1)k22n2kC2nk+1k

a_n=2^{2n}+\sum_{k=1}^{n}(-1)^k2^{2n-2k}(C_{2n-k}^k+C_{2n-k}^{k-1})=\sum_{k=0}^{n}(-1)^k2^{2n-2k}C_{2n-k}^k+\sum_{k=0}^{n-1}(-1)^{k+1}2^{2(n-1)-2k}C_{2(n-1)-k+1}^kan=22n+k=1n(1)k22n2k(C2nkk+C2nkk1)=k=0n(1)k22n2kC2nkk+k=0n1(1)k+122(n1)2kC2(n1)k+1k

b_n=\sum_{k=0}^{n}(-1)^k2^{2n-2k}C_{2n-k}^kbn=k=0n(1)k22n2kC2nkk,则b_n=a_n+a_{n-1}bn=an+an1

b_n=2^{2n}+\sum_{k=1}^{n-1}(-1)^k2^{2n-2k}(C_{2n-k-1}^k+C_{2n-k-1}^{k-1})+(-1)^nbn=22n+k=1n1(1)k22n2k(C2nk1k+C2nk1k1)+(1)n

=4\sum_{k=0}^{n-1}(-1)^k2^{2(n-1)-2k}C_{2(n-1)-k+1}^k+\sum_{k=0}^{n-1}(-1)^{k+1}2^{2(n-1)-2k}C_{2(n-1)-k}^k=4k=0n1(1)k22(n1)2kC2(n1)k+1k+k=0n1(1)k+122(n1)2kC2(n1)kk

=4a_n-b_{n-1}=4anbn1

a_n-a_{n-1}=a_{n-1}-a_{n-2}anan1=an1an2。因为a_0=1,a_1=1a0=1,a1=1,所以a_n=n+1an=n+1

证明比较费时,打表找规律能很快的得出解,所以本题的关键在于打表找规律。

一开始觉得这题有毒,谁都知道应该要先求出fn(x)的值,但是求这个是难点,但是没办法,该求的还是得求,于是将样例中的几组求了一下(比较傻,没想到打表)



故可大胆猜测出
然后用一下欧拉函数的模板就ok了
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#define exp 1e-10
using namespace std;
const int N = 50005;
const int M = 710;
const int inf = 1000000000;
const int mod = 1000000007;
__int64 euler(__int64 n)
{
    __int64 ans=1;
    __int64 i;
    for(i=2;i*i<=n;i++)
    {
        if(n%i==0)
        {
            n/=i;
            ans*=i-1;
            while(n%i==0)
            {
                n/=i;
                ans*=i;
            }
        }
    }
    if(n>1)
        ans*=n-1;
    return ans;
}
int main()
{
    __int64 n,x;
    while(~scanf("%I64d%I64d",&n,&x))
        printf("%I64d\n",euler(n+x+1));
    return 0;
}
菜鸟成长记

你可能感兴趣的:(ACM)