素数判断(试除法求质因数)
发布时间
阅读量:
阅读量

思路:试除法求素数
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include<iostream>
#include<vector>
#include<queue>
//#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define SIS std::ios::sync_with_stdio(false)
#define space putchar(' ')
#define enter putchar('\n')
#define lson root<<1
#define rson root<<1|1
typedef pair<int,int> PII;
const int mod=1e9+7;
const int N=2e6+10;
const int inf=0x7f7f7f7f;
const int maxx=2e5+7;
ll gcd(ll a,ll b)
{
return b==0?a:gcd(b,a%b);
}
ll lcm(ll a,ll b)
{
return a*(b/gcd(a,b));
}
template <class T>
void read(T &x)
{
char c;
bool op = 0;
while(c = getchar(), c < '0' || c > '9')
if(c == '-')
op = 1;
x = c - '0';
while(c = getchar(), c >= '0' && c <= '9')
x = x * 10 + c - '0';
if(op)
x = -x;
}
template <class T>
void write(T x)
{
if(x < 0)
x = -x, putchar('-');
if(x >= 10)
write(x / 10);
putchar('0' + x % 10);
}
ll qsm(int a,int b,int p)
{
ll res=1%p;
while(b)
{
if(b&1) res=res*a%p;
a=1ll*a*a%p;
b>>=1;
}
return res;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,x;
read(x);
n=sqrt(x);
int flag=0;
vector<int> vt;
for(int i=2;i<=n;i++)
{
if(x%i==0)
{
flag=1;
vt.push_back(i);
while(x%i==0)
{
x/=i;
}
}
if(x==1)break;
}
if(flag==0)
{
printf("isprime\n%d\n",x);
}
else
{
printf("noprime\n");
for(int i=0;i<vt.size()-1;i++)
{
printf("%d ",vt[i]);
}
printf("%d",vt[vt.size()-1]);
if(x!=1)
{
printf(" %d",x);
}
printf("\n");
}
}
return 0;
}
/*
1
3 2
1 1 2 3 4
1 0 1 2 3
2 1 2 3 4
*/
全部评论 (0)
还没有任何评论哟~
