uestc oj 1151 The simple problem

A Simple Problem
Time Limit: 1000 ms Memory Limit: 65535 kB Solved: 96 Tried: 449
Submit Status Best Solution Back
Description
Given a nonnegative integer a, and a positive integer N, we define:


f(a, 1) = a 

f(a, k) = f(a, k – 1) * f(a, k – 1) % N, k > 1


There may or may not exist some positive integer k satisfying f(a, k) = 0.


Your task is, given a positive integer N, to determine how many a (0 ≤ a ≤ N) there are, such that for some positive integer k, f(a, k) = 0.

Input
The input contains an integer T on the first line, indicating the number of test cases. Each test case contains only one positive integer N (1 ≤ N ≤ 1000000000) on a line.

Output
For each test case, output the answer on a single line.

Sample Input
6
2
12
50
180
245
361

Sample Output
2
3
6
7
8
20

Source
The 5th UESTC Programming Contest Final

满足f(a,k)=0等价于a具有N的全部素因子。因为平方可使素因子数增多并最后>=N的相应素因子数,

而平方本身不会产生新的素因子。求模可以最后来做,因为(a mod N) * b mod N = a * b mod N。

设N的全部素因子乘积(每个只取一次)为P,那么a一定是P的倍数。答案是N / P + 1。

#include<stdio.h>
using namespace std;
int T,N;
bool isPrime(int b)
{
    for(int j=2;j<b/2+1;j++)
    {
        if(b%j==0)
        return false;
    }
    return true;
}
int find(int a)
{
    int result =1;
    if(a==1)
    return 1;
    else if(isPrime(a))
    return a;
    else
    {
        for(int i=2;i<=a;)
        {
            if(a%i==0&&isPrime(i))
            {
                a/=i;
                result *= i;
                i++;
            }
            else i++;
        }
        return result;
    }
}

int main()
{
   scanf("%d",&T);
   while(T--)
   {
       scanf("%d",&N);
       int k = find(N);
       printf("%d\n",N/k+1);
   }
   return 0;
}
提交的时候花了486ms  不知道那些0ms的大神是怎么解决的。。。。


你可能感兴趣的:(数论)