HDU - 2973 - YAPTCHA

本文介绍了一种名为YAPTCHA的数学难题,该难题用于区分人类与计算机。文章详细解析了如何利用筛素数和威尔逊定理解决此问题,并提供了两种实现方案,一种采用标准输入输出,另一种则优化了输入输出过程以提高效率。

先上题目:

YAPTCHA

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 463    Accepted Submission(s): 280


Problem Description
The math department has been having problems lately. Due to immense amount of unsolicited automated programs which were crawling across their pages, they decided to put Yet-Another-Public-Turing-Test-to-Tell-Computers-and-Humans-Apart on their webpages. In short, to get access to their scientific papers, one have to prove yourself eligible and worthy, i.e. solve a mathematic riddle.


However, the test turned out difficult for some math PhD students and even for some professors. Therefore, the math department wants to write a helper program which solves this task (it is not irrational, as they are going to make money on selling the program).

The task that is presented to anyone visiting the start page of the math department is as follows: given a natural n, compute

where [x] denotes the largest integer not greater than x.
 

 

Input
The first line contains the number of queries t (t <= 10^6). Each query consist of one natural number n (1 <= n <= 10^6).
 

 

Output
For each n given in the input output the value of Sn.
 

 

Sample Input
13 1 2 3 4 5 6 7 8 9 10 100 1000 10000
 

 

Sample Output
0 1 1 2 2 2 2 3 3 4 28 207 1609
 
  题意很简单,t个case,每个case给你一个n,根据公式求出结果然后直接输出结果。
  先说一下这一题需要用到什么,一是筛素数,二是威尔逊定理。
  威尔逊定理如下:
          p是素数,则 (p-1)! ≡ -1 (mod p)
  为什么需要用到威尔逊定理,通过观察题目给出的公式可以令p=3k+7,则通过分析,当p为素数的时候Sn中第k个数就是1,否则p不是素数的时候第k个数就是0。所以才需要筛素数,然后再判断给出的范围里面每一个Sn,然后每一次查询就直接输出答案。
  在ac之前wa了两次,后来发现是数组开小了= =了,所以这里开大100其实没有关系。
  代码上两份,一份是完全自己写的,筛素数的时间复杂度为nloglogn,提交以后返回的时间是800+ms,另一份是用上人家的输入输出挂,速度会去到200+ms,染过吧筛素数的方法换成线性筛法的话还会快大概30ms。
 
上代码:
#include <stdio.h>
#include <string.h>
#define MAX 1000110
using namespace std;

bool pri[MAX*3];
int ans[MAX];

void dedeal()
{
    long long i,j,n;
    n=(MAX-10)*3;
    memset(pri,0,sizeof(pri));
    pri[0]=pri[1]=1;
    for(i=2;i<=n;i++)
    {
        if(!pri[i])
           for(j=i*i;j<=n;j+=i) pri[j]=1;
    }
}

void deal()
{
    int i,n;
    n=(MAX-10);
    memset(ans,0,sizeof(ans));
    for(i=1;i<=n;i++)
    {
        if(!pri[3*i+7])
            ans[i]=ans[i-1]+1;
        else ans[i]=ans[i-1];
    }
}

int main()
{
    int n,t;
    //freopen("data.txt","r",stdin);
    dedeal();
    deal();
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d\n",&n);
        printf("%d\n",ans[n]);
    }
    return 0;
}
2973

 

提速版

 1 #include <iostream>
 2 #include <string.h>
 3 #include <stdio.h>
 4 #define MAX 1000110
 5 using namespace std ;
 6 bool pri[3000008] ;
 7 int ans[1000001] ;
 8 
 9 
10 void dedeal()
11 {
12     long long i,j,n;
13     n=(MAX-10)*3;
14     memset(pri,0,sizeof(pri));
15     pri[0]=pri[1]=1;
16     for(i=2;i<=n;i++)
17     {
18         if(!pri[i])
19            for(j=i*i;j<=n;j+=i) pri[j]=1;
20     }
21 }
22 
23 void deal()
24 {
25     int i,n;
26     n=(MAX-10);
27     memset(ans,0,sizeof(ans));
28     for(i=1;i<=n;i++)
29     {
30         if(!pri[3*i+7])
31             ans[i]=ans[i-1]+1;
32         else ans[i]=ans[i-1];
33     }
34 }
35 
36 
37 inline bool scan_d(int &num)
38 {
39         char in;bool IsN=false;
40         in=getchar();
41         if(in==EOF) return false;
42         while(in!='-'&&(in<'0'||in>'9')) in=getchar();
43         if(in=='-'){ IsN=true;num=0;}
44         else num=in-'0';
45         while(in=getchar(),in>='0'&&in<='9'){
46                 num*=10,num+=in-'0';
47         }
48         if(IsN) num=-num;
49         return true;
50 }
51 
52 void print_f(int x){
53     if(x==0)return;
54     print_f(x/10);
55     putchar(x%10+'0');
56 }
57 int main()
58 {
59     //freopen("data.txt","r",stdin);
60     int t ;
61     dedeal();
62     deal();
63     scan_d(t) ;
64     while(t--)
65     {
66         int n ;
67         scan_d(n) ;
68         if(n==1)
69             putchar('0') ;
70         else
71             print_f(ans[n]) ;
72         putchar('\n') ;
73     }
74     return 0 ;
75 }
2973

 

转载于:https://www.cnblogs.com/sineatos/p/3269140.html

### 关于HDU - 6609 的题目解析 由于当前未提供具体关于 HDU - 6609 题目的详细描述,以下是基于一般算法竞赛题型可能涉及的内容进行推测和解答。 #### 可能的题目背景 假设该题目属于动态规划类问题(类似于多重背包问题),其核心在于优化资源分配或路径选择。此类问题通常会给出一组物品及其属性(如重量、价值等)以及约束条件(如容量限制)。目标是最优地选取某些物品使得满足特定的目标函数[^2]。 #### 动态转移方程设计 如果此题确实是一个变种的背包问题,则可以采用如下状态定义方法: 设 `dp[i][j]` 表示前 i 种物品,在某种条件下达到 j 值时的最大收益或者最小代价。对于每一种新加入考虑范围内的物体 k ,更新规则可能是这样的形式: ```python for i in range(n): for s in range(V, w[k]-1, -1): dp[s] = max(dp[s], dp[s-w[k]] + v[k]) ``` 这里需要注意边界情况处理以及初始化设置合理值来保证计算准确性。 另外还有一种可能性就是它涉及到组合数学方面知识或者是图论最短路等相关知识点。如果是后者的话那么就需要构建相应的邻接表表示图形结构并通过Dijkstra/Bellman-Ford/Floyd-Warshall等经典算法求解两点间距离等问题了[^4]。 最后按照输出格式要求打印结果字符串"Case #X: Y"[^3]。 #### 示例代码片段 下面展示了一个简单的伪代码框架用于解决上述提到类型的DP问题: ```python def solve(): t=int(input()) res=[] cas=1 while(t>0): n,k=list(map(int,input().split())) # Initialize your data structures here ans=find_min_unhappiness() # Implement function find_min_unhappiness() res.append(f'Case #{cas}: {round(ans)}') cas+=1 t-=1 print("\n".join(res)) solve() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值