uestc oj 1151 The simple problem

本文介绍了一种算法,用于解决给定正整数N时,找出满足特定递归定义条件下f(a,k)=0的非负整数a的数量。通过分解N的素因子,算法能够高效地计算出符合条件的a的总数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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的大神是怎么解决的。。。。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值