算法学习笔记:质数判定 + 质数分解 + 筛质数 + 求约数 + 约数个数 + 约束的和 + 最大公约数

质数判定

适用场景,有10^5个测试点,并且每个测试点需要判断一个>10 ^6的数是否是质数,如果单纯使用根号n的方法是会超时的,所以需要更快的质数判定法

在这里插入图片描述

试除法判定质数

bool isPrime(int x)
{
    if (x < 2) return false;
    for (int i = 2; i <= x / i; i ++) //只需要枚举较小的因子即可,这里需要等于号(平方的情况)
    {
        if (x % i == 0) return false;
    }
    return true;
}

试除法分解质因数

void divide(int x)
{
    for (int i = 2; i <= x / i; i ++) //遍历x较小的质因数
    {
        if (x % i == 0)	
        {
            int c = 0;
            while (x % i == 0)
            {
                x /= i;
                c ++;
            }
            cout << i << " " << c << endl;
        }
    }
    if (x > 1) cout << x << " " << 1 << endl; //>sqrt(x)的质因数最多只有可能有一个
}

在这里插入图片描述

朴素法筛质数

for (int i = 2; i <= n; i ++)
{
    if (!st[i]) res ++; //质数个数加一
    for (int j = i + i; j <= n; j += i) st[j] = true; //删去每一个数的倍数
}

埃式筛质数

for (int i = 2; i <= n; i ++)
{
    if (!st[i])
    {
        res ++; //质数个数加一
        for (int j = i + i; j <= n; j += i) st[j] = true; //删去质数的倍数
    }
}

线性法筛质数

for (int i = 2; i <= n; i ++)
{
    if (!st[i])
    {
        res ++;//质数个数加一
        p[cnt ++] = i; //从小到大记录个数
    }
    for (int j = 0; p[j] <= n / i; j ++)
    {
        st[p[j] * i] = true;
        if (i % p[j] == 0) break;
    }
}

在这里插入图片描述

试除法求约数

vector <int> res;
for (int i = 1; i <= x / i; i ++)
{
    if (x % i == 0)
    {
        res.push_back(i);
        if (x / i != i) res.push_back(x / i);
    }
}

公式法求约数个数

long long res = 1;
unordered_map <int, int> m;
while (n --)
{
    int x;
    cin >> x;
    
	//分解质因数,记录对应指数
    for (int i = 2; i <= x / i; i ++)
    {
        while (x % i == 0)
        {
            x /= i;
            m[i] ++;
        }
    }
    if (x > 1) m[x] ++;
}
//对每一个指数加一求乘积
for (auto x : m)
{
    res = res * (x.second + 1) % mod;
}

公式法求约数和

long long res = 1;
unordered_map <int, int> m;
//分解质因数,记录其指数
while (n --)
{
    int x;
    cin >> x;
    
    for (int i = 2; i <= x / i; i ++)
    {
        while (x % i == 0)
        {
            x /= i;
            m[i] ++;
        }
    }
    if (x > 1) m[x] ++;
}
//求多项式并求乘积
for (auto x : m)
{
    int a = x.first, b = x.second;
    long long t = 1;
    while (b --) t = (t * a + 1) % mod;
    res = res * t % mod;
}

辗转相除法求最大公约数

//(a, b) = (b, a % b) 如果b大于0,返回gcd(b, a % b), 否则(0, a) 返回a
int gcd(int a, int b)
{
    return b ? gcd(b, a % b) : a;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot 是一个快速开发框架,它提供了一系列的工具和插件,可以快速构建一个企业级的应用程序。而 Shiro 是一个强大而灵活的安全框架,可以提供身份验证、授权、密码加密、会话管理等功能。CAS 是一个单点登录(SSO)协议,可以实现用户在多个应用系统中使用同一个身份验证。 下面是一个简单的 Spring Boot + Shiro + CAS 的示例应用程序: 1. 创建一个 Spring Boot 应用程序,并添加依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring-boot-web-starter</artifactId> <version>1.5.3</version> </dependency> <dependency> <groupId>org.jasig.cas.client</groupId> <artifactId>cas-client-core</artifactId> <version>3.6.0</version> </dependency> ``` 2. 配置 Shiro: ```java @Configuration public class ShiroConfig { @Bean public CasRealm casRealm() { CasRealm realm = new CasRealm(); realm.setCasServerUrlPrefix("https://cas.example.com/cas"); realm.setCasService("https://myapp.example.com/cas"); realm.setDefaultRoles("user"); realm.setRoleAttributeNames("memberOf"); return realm; } @Bean public DefaultWebSecurityManager securityManager() { DefaultWebSecurityManager manager = new DefaultWebSecurityManager(); manager.setRealm(casRealm()); return manager; } @Bean public ShiroFilterFactoryBean shiroFilter() { ShiroFilterFactoryBean filter = new ShiroFilterFactoryBean(); filter.setSecurityManager(securityManager()); filter.setLoginUrl("https://cas.example.com/cas/login?service=https://myapp.example.com/cas"); filter.setSuccessUrl("/home"); filter.setUnauthorizedUrl("/403"); filter.setFilterChainDefinitionMap(Collections.singletonMap("/**", "authc")); return filter; } } ``` 3. 配置 CAS: ```java @Configuration public class CasConfig { @Bean public CasAuthenticationFilter casAuthenticationFilter() { CasAuthenticationFilter filter = new CasAuthenticationFilter(); filter.setCasServerLoginUrl("https://cas.example.com/cas/login"); filter.setServerName("https://myapp.example.com/cas"); filter.setAuthenticationSuccessHandler(authenticationSuccessHandler()); filter.setAuthenticationFailureHandler(authenticationFailureHandler()); return filter; } @Bean public SimpleUrlAuthenticationSuccessHandler authenticationSuccessHandler() { SimpleUrlAuthenticationSuccessHandler handler = new SimpleUrlAuthenticationSuccessHandler(); handler.setDefaultTargetUrl("/home"); handler.setAlwaysUseDefaultTargetUrl(true); return handler; } @Bean public SimpleUrlAuthenticationFailureHandler authenticationFailureHandler() { SimpleUrlAuthenticationFailureHandler handler = new SimpleUrlAuthenticationFailureHandler(); handler.setDefaultFailureUrl("/login?error=true"); return handler; } @Bean public FilterRegistrationBean<CasAuthenticationFilter> casFilterRegistrationBean() { FilterRegistrationBean<CasAuthenticationFilter> registration = new FilterRegistrationBean<>(); registration.setFilter(casAuthenticationFilter()); registration.addUrlPatterns("/*"); registration.setName("CAS Authentication Filter"); registration.setOrder(1); return registration; } @Bean public ServletListenerRegistrationBean<SingleSignOutHttpSessionListener> singleSignOutHttpSessionListener() { ServletListenerRegistrationBean<SingleSignOutHttpSessionListener> registration = new ServletListenerRegistrationBean<>(); registration.setListener(new SingleSignOutHttpSessionListener()); registration.setOrder(2); return registration; } @Bean public ServletRegistrationBean<Servlet> casValidationServletRegistrationBean() { ServletRegistrationBean<Servlet> registration = new ServletRegistrationBean<>(); registration.setServlet(new Cas20ProxyReceivingTicketValidationFilter()); registration.addUrlMappings("/cas/*"); registration.setName("CAS Validation Filter"); registration.setOrder(3); return registration; } } ``` 4. 创建一个 HomeController: ```java @Controller public class HomeController { @GetMapping("/home") public String home() { return "home"; } @GetMapping("/403") public String error403() { return "403"; } } ``` 5. 创建一个 CAS 认证服务器: ```java @SpringBootApplication public class CasServerApplication { public static void main(String[] args) { SpringApplication.run(CasServerApplication.class, args); } } ``` 6. 创建一个 CAS 客户端: ```java @SpringBootApplication @EnableScheduling public class CasClientApplication { public static void main(String[] args) { SpringApplication.run(CasClientApplication.class, args); } } ``` 这就是一个简单的 Spring Boot + Shiro + CAS 的示例应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只可爱的小猴子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值