codevs 月赛1

题目http://pan.baidu.com/s/1gdfb4bh

t1水题

 1 #include<bits/stdc++.h>
 2 #define ok(a,l,r) (((l)<=(a))&&((a)<=(r)))
 3 #define clr(a,x) memset(a,x,sizeof(a))
 4 #define rep(i,l,r) for(int i=l;i<r;i++)
 5 typedef long long ll;
 6 using namespace std;
 7 int read()
 8 {
 9     char c=getchar();
10     int ans=0,f=1;
11     while(!isdigit(c)){
12         if(c=='-') f=-1;
13         c=getchar();
14     }
15     while(isdigit(c)){
16         ans=ans*10+c-'0';
17         c=getchar();
18     }
19     return ans*f;
20 }
21 int getsum(int a){
22     int ans=0;
23     while(a){
24         ans+=a%10;
25         a/=10;
26     }
27     return ans;
28 }
29 const int maxs=100;
30 int ans[maxs];
31 vector<int>hehe;
32 int main()
33 {    
34     int k=read(),p=read(),q=read(),l=read(),r=read(),cnt=0,cnt1=0;
35     rep(i,1,82){
36         int t=pow(i,k)*p+q;
37         if(getsum(t)==i){
38             ans[cnt++]=t;
39         }
40     }
41     sort(ans,ans+cnt);
42     rep(i,0,cnt){
43         if(ok(ans[i],l,r)) hehe.push_back(ans[i]);
44     }
45     printf("%d\n",hehe.size());
46     rep(i,0,hehe.size()){
47         printf("%d",hehe[i]);
48         if(i!=hehe.size()-1) putchar(' ');
49     }
50     return 0;
51 }
View Code

t2kmp

 1 #include<bits/stdc++.h>
 2 #define clr(a,x) memset(a,x,sizeof(a))
 3 #define rep(i,l,r) for(int i=l;i<r;i++)
 4 typedef long long ll;
 5 using namespace std;
 6 int read()
 7 {
 8     char c=getchar();
 9     int ans=0,f=1;
10     while(!isdigit(c)){
11         if(c=='-') f=-1;
12         c=getchar();
13     }
14     while(isdigit(c)){
15         ans=ans*10+c-'0';
16         c=getchar();
17     }
18     return ans*f;
19 }
20 const int maxn=1000009;
21 int n,m,a[maxn],b[maxn],A[maxn],B[maxn],next[maxn];
22 void getNext()
23 {
24     int j,k;
25     j=0;k=-1;next[0]=-1;
26     while(j<n){
27         if(k==-1||A[j]==A[k])
28             next[++j]=++k;
29         else
30             k=next[k];
31     }
32 }
33 int KMP_Count()
34 {    
35     /*rep(i,0,n) cout<<A[i]<<' ';
36     cout<<endl;
37     rep(i,0,m) cout<<B[i]<<' ';
38     cout<<endl;*/
39     int ans=0;
40     int i,j=0;
41     if(m==1&&n==1)
42     {
43         if(B[0]==A[0])
44             return 1;
45         else
46             return 0;
47     }
48     getNext();
49     for(i=0;i<m;i++)
50     {
51         while(j>0&&B[i]!=A[j])
52             j=next[j];
53         if(B[i]==A[j])
54             j++;
55         if(j==n)
56         {
57             ans++;
58             j=next[j];
59         }
60     }
61     return n?ans:m+1;
62 }
63 int main()
64 {
65     n=read(),m=read();
66     clr(a,0),clr(b,0),clr(A,0),clr(B,0);
67     rep(i,0,n) a[i]=read();
68     rep(i,0,m) b[i]=read();
69     --n;--m;
70     rep(i,0,n) A[i]=a[i+1]-a[i];
71     rep(i,0,m) B[i]=b[i+1]-b[i];
72     int sum=KMP_Count();
73     clr(A,0);
74     sum+=KMP_Count();
75     rep(i,0,m>>1) swap(B[i],B[m-i-1]);
76     sum+=KMP_Count();
77     rep(i,0,n) A[i]=a[i+1]-a[i];
78     sum+=KMP_Count();
79     printf("%d\n",sum);
80     return 0;
81 }
View Code

t3不会

posted @ 2015-07-26 16:16  ChenThree  阅读(228)  评论(1编辑  收藏  举报