考试题 T1,T2,T3
T1
【题目描述】
小林和亮亮正在玩一个取石子的游戏。 石子一共有 n 堆,其中第 i 堆恰好有 i 粒石子。小林先取,亮亮后取,并且 两人依次轮流取石。每一次取石子的人可以选择任意一堆还未被取完的石子,并 取走这一堆中任意多粒石子(注意,不能一粒石子也不取,也不能同时在多堆石 子中取石)。最终,无石可取的人为败。 小林和亮亮都十分聪明,他们的每次取石都会采取最优策略。在经过多次游 戏后,小林发现了先手必胜的条件,但他不满足于此,他想知道,在知道石子的 堆数 n 后,他第一次取石有多少种方式可以获胜。
【输入格式】
第一行一个整数 T,表示数据组数。 接下来 T 行每行一个整数 n,表示石子的堆数。
【输出格式】
每组数据输出一个整数,表示在两个人都采用最佳策略的情况下,小林第一 次取石能够获胜的方式数,如小林必败,则输出 0。
【样例输入】
2 2 3
【样例输出】
1 0
【样例解释】
n=2 时小林只有一种取胜方式,即取在第二堆石子中取一粒。 n=3 时小林必败,因此输出 0。
【数据规模】
对于 20%的数据, n<=10; 对于 50%的数据, n<=1000; 对于 90%的数据, n<=10^15; 对于 100%的数据, 1<=n<=10^1000, 1<=T<=10
题意分析
首先 这是一道博弈论求解方案数的题
大致观察题意之后 就可以看出这是Nim游戏
1.Nim游戏的规律
a1^a2^a3^......^an==0 ? 后手必胜 : 先手必胜
2.先手必胜方案
a1^a2^a3^......^an=k
因为k^k=0
使得ai^k<ai 则使后手得以面临必败态
50pts
直接O(n)遍历 符合要求则++
90pts
由于是1到n的xor 所以我们寻找规律
以下为从1~n的异或和:
i xor
1 1
2 3
3 0
4 4
5 1
6 7
7 0
8 8
9 1
10 11
11 0
12 12
打表找规律在对付数论题方面十分管用
我们发现
n%4==1 xor和为1
n%4==2 xor和为n+1
n%4==3 xor和为0
n%4==4 xor和为n
1.n%4==3 后手必败
2.n%4==1 我们需要寻找异或1小于其本身的数
1 ^ 1 =0
2 ^ 1 =3
3 ^ 1 =2
4 ^ 1 =5
5 ^ 1 =4
6 ^ 1 =7
7 ^ 1 =6
8 ^ 1 =9
9 ^ 1 =10
10 ^ 1 =11
n%4==1 所以n必定是奇数
所以结果就是(n+1)/2
3.n%4==2 异或和为(n+1)
我们再次打表进行统计
now 1 smaller is 0
now 2 smaller is 1
now 3 smaller is 0
now 4 smaller is 1
now 5 smaller is 2
now 6 smaller is 3
now 7 smaller is 0
now 8 smaller is 1
now 9 smaller is 2
now 10 smaller is 3
now 11 smaller is 4
now 12 smaller is 5
now 13 smaller is 6
now 14 smaller is 7
now 15 smaller is 0
now 16 smaller is 1
now 17 smaller is 2
now 18 smaller is 3
now 19 smaller is 4
now 20 smaller is 5
now 21 smaller is 6
now 22 smaller is 7
now 23 smaller is 8
now 24 smaller is 9
now 25 smaller is 10
now 26 smaller is 11
now 27 smaller is 12
now 28 smaller is 13
now 29 smaller is 14
now 30 smaller is 15
now 31 smaller is 0
now 32 smaller is 1
now 33 smaller is 2
now 34 smaller is 3
now 35 smaller is 4
now 36 smaller is 5
now 37 smaller is 6
now 38 smaller is 7
now 39 smaller is 8
now 40 smaller is 9
now 41 smaller is 10
now 42 smaller is 11
now 43 smaller is 12
now 44 smaller is 13
now 45 smaller is 14
now 46 smaller is 15
now 47 smaller is 16
now 48 smaller is 17
now 49 smaller is 18
now 50 smaller is 19
now 51 smaller is 20
now 52 smaller is 21
now 53 smaller is 22
now 54 smaller is 23
now 55 smaller is 24
now 56 smaller is 25
now 57 smaller is 26
now 58 smaller is 27
now 59 smaller is 28
now 60 smaller is 29
now 61 smaller is 30
now 62 smaller is 31
now 63 smaller is 0
now 64 smaller is 1
now 65 smaller is 2
now 66 smaller is 3
now 67 smaller is 4
now 68 smaller is 5
now 69 smaller is 6
now 70 smaller is 7
now 71 smaller is 8
now 72 smaller is 9
now 73 smaller is 10
now 74 smaller is 11
now 75 smaller is 12
now 76 smaller is 13
now 77 smaller is 14
now 78 smaller is 15
now 79 smaller is 16
now 80 smaller is 17
now 81 smaller is 18
now 82 smaller is 19
now 83 smaller is 20
now 84 smaller is 21
now 85 smaller is 22
now 86 smaller is 23
now 87 smaller is 24
now 88 smaller is 25
now 89 smaller is 26
now 90 smaller is 27
now 91 smaller is 28
now 92 smaller is 29
now 93 smaller is 30
now 94 smaller is 31
now 95 smaller is 32
now 96 smaller is 33
now 97 smaller is 34
now 98 smaller is 35
now 99 smaller is 36
now 100 smaller is 37
now 101 smaller is 38
now 102 smaller is 39
now 103 smaller is 40
now 104 smaller is 41
now 105 smaller is 42
now 106 smaller is 43
now 107 smaller is 44
now 108 smaller is 45
now 109 smaller is 46
now 110 smaller is 47
now 111 smaller is 48
now 112 smaller is 49
now 113 smaller is 50
now 114 smaller is 51
now 115 smaller is 52
now 116 smaller is 53
now 117 smaller is 54
now 118 smaller is 55
now 119 smaller is 56
now 120 smaller is 57
now 121 smaller is 58
now 122 smaller is 59
now 123 smaller is 60
now 124 smaller is 61
now 125 smaller is 62
now 126 smaller is 63
now 127 smaller is 0
now 128 smaller is 1
now 129 smaller is 2
now 130 smaller is 3
now 131 smaller is 4
now 132 smaller is 5
now 133 smaller is 6
now 134 smaller is 7
now 135 smaller is 8
now 136 smaller is 9
now 137 smaller is 10
now 138 smaller is 11
now 139 smaller is 12
now 140 smaller is 13
now 141 smaller is 14
now 142 smaller is 15
now 143 smaller is 16
now 144 smaller is 17
now 145 smaller is 18
now 146 smaller is 19
now 147 smaller is 20
now 148 smaller is 21
now 149 smaller is 22
now 150 smaller is 23
now 151 smaller is 24
now 152 smaller is 25
now 153 smaller is 26
now 154 smaller is 27
now 155 smaller is 28
now 156 smaller is 29
now 157 smaller is 30
now 158 smaller is 31
now 159 smaller is 32
now 160 smaller is 33
now 161 smaller is 34
now 162 smaller is 35
now 163 smaller is 36
now 164 smaller is 37
now 165 smaller is 38
now 166 smaller is 39
now 167 smaller is 40
now 168 smaller is 41
now 169 smaller is 42
now 170 smaller is 43
now 171 smaller is 44
now 172 smaller is 45
now 173 smaller is 46
now 174 smaller is 47
now 175 smaller is 48
now 176 smaller is 49
now 177 smaller is 50
now 178 smaller is 51
now 179 smaller is 52
now 180 smaller is 53
now 181 smaller is 54
now 182 smaller is 55
now 183 smaller is 56
now 184 smaller is 57
now 185 smaller is 58
now 186 smaller is 59
now 187 smaller is 60
now 188 smaller is 61
now 189 smaller is 62
now 190 smaller is 63
now 191 smaller is 64
now 192 smaller is 65
now 193 smaller is 66
now 194 smaller is 67
now 195 smaller is 68
now 196 smaller is 69
now 197 smaller is 70
now 198 smaller is 71
now 199 smaller is 72
now 200 smaller is 73
由于此时(n%4==2) n是一个偶数
所以我们发现例如
256的对应值为1 也就是256+1之后减去二进制最高位对应值
130(10)=10000010(2)
同理 130对应值=(130+1)-(1<<7)
所以可以直接使用幂函数和log函数求值
4.n%4==0 异或和为n
继续打表
now 1 smaller is 1
now 2 smaller is 1
now 3 smaller is 2
now 4 smaller is 1
now 5 smaller is 2
now 6 smaller is 3
now 7 smaller is 4
now 8 smaller is 1
now 9 smaller is 2
now 10 smaller is 3
now 11 smaller is 4
now 12 smaller is 5
now 13 smaller is 6
now 14 smaller is 7
now 15 smaller is 8
now 16 smaller is 1
now 17 smaller is 2
now 18 smaller is 3
now 19 smaller is 4
now 20 smaller is 5
now 21 smaller is 6
now 22 smaller is 7
now 23 smaller is 8
now 24 smaller is 9
now 25 smaller is 10
now 26 smaller is 11
now 27 smaller is 12
now 28 smaller is 13
now 29 smaller is 14
now 30 smaller is 15
now 31 smaller is 16
now 32 smaller is 1
now 33 smaller is 2
now 34 smaller is 3
now 35 smaller is 4
now 36 smaller is 5
now 37 smaller is 6
now 38 smaller is 7
now 39 smaller is 8
now 40 smaller is 9
now 41 smaller is 10
now 42 smaller is 11
now 43 smaller is 12
now 44 smaller is 13
now 45 smaller is 14
now 46 smaller is 15
now 47 smaller is 16
now 48 smaller is 17
now 49 smaller is 18
now 50 smaller is 19
now 51 smaller is 20
now 52 smaller is 21
now 53 smaller is 22
now 54 smaller is 23
now 55 smaller is 24
now 56 smaller is 25
now 57 smaller is 26
now 58 smaller is 27
now 59 smaller is 28
now 60 smaller is 29
now 61 smaller is 30
now 62 smaller is 31
now 63 smaller is 32
now 64 smaller is 1
now 65 smaller is 2
now 66 smaller is 3
now 67 smaller is 4
now 68 smaller is 5
now 69 smaller is 6
now 70 smaller is 7
now 71 smaller is 8
now 72 smaller is 9
now 73 smaller is 10
now 74 smaller is 11
now 75 smaller is 12
now 76 smaller is 13
now 77 smaller is 14
now 78 smaller is 15
now 79 smaller is 16
now 80 smaller is 17
now 81 smaller is 18
now 82 smaller is 19
now 83 smaller is 20
now 84 smaller is 21
now 85 smaller is 22
now 86 smaller is 23
now 87 smaller is 24
now 88 smaller is 25
now 89 smaller is 26
now 90 smaller is 27
now 91 smaller is 28
now 92 smaller is 29
now 93 smaller is 30
now 94 smaller is 31
now 95 smaller is 32
now 96 smaller is 33
now 97 smaller is 34
now 98 smaller is 35
now 99 smaller is 36
now 100 smaller is 37
now 101 smaller is 38
now 102 smaller is 39
now 103 smaller is 40
now 104 smaller is 41
now 105 smaller is 42
now 106 smaller is 43
now 107 smaller is 44
now 108 smaller is 45
now 109 smaller is 46
now 110 smaller is 47
now 111 smaller is 48
now 112 smaller is 49
now 113 smaller is 50
now 114 smaller is 51
now 115 smaller is 52
now 116 smaller is 53
now 117 smaller is 54
now 118 smaller is 55
now 119 smaller is 56
now 120 smaller is 57
now 121 smaller is 58
now 122 smaller is 59
now 123 smaller is 60
now 124 smaller is 61
now 125 smaller is 62
now 126 smaller is 63
now 127 smaller is 64
now 128 smaller is 1
now 129 smaller is 2
now 130 smaller is 3
now 131 smaller is 4
now 132 smaller is 5
now 133 smaller is 6
now 134 smaller is 7
now 135 smaller is 8
now 136 smaller is 9
now 137 smaller is 10
now 138 smaller is 11
now 139 smaller is 12
now 140 smaller is 13
now 141 smaller is 14
now 142 smaller is 15
now 143 smaller is 16
now 144 smaller is 17
now 145 smaller is 18
now 146 smaller is 19
now 147 smaller is 20
now 148 smaller is 21
now 149 smaller is 22
now 150 smaller is 23
now 151 smaller is 24
now 152 smaller is 25
now 153 smaller is 26
now 154 smaller is 27
now 155 smaller is 28
now 156 smaller is 29
now 157 smaller is 30
now 158 smaller is 31
now 159 smaller is 32
now 160 smaller is 33
now 161 smaller is 34
now 162 smaller is 35
now 163 smaller is 36
now 164 smaller is 37
now 165 smaller is 38
now 166 smaller is 39
now 167 smaller is 40
now 168 smaller is 41
now 169 smaller is 42
now 170 smaller is 43
now 171 smaller is 44
now 172 smaller is 45
now 173 smaller is 46
now 174 smaller is 47
now 175 smaller is 48
now 176 smaller is 49
now 177 smaller is 50
now 178 smaller is 51
now 179 smaller is 52
now 180 smaller is 53
now 181 smaller is 54
now 182 smaller is 55
now 183 smaller is 56
now 184 smaller is 57
now 185 smaller is 58
now 186 smaller is 59
now 187 smaller is 60
now 188 smaller is 61
now 189 smaller is 62
now 190 smaller is 63
now 191 smaller is 64
now 192 smaller is 65
now 193 smaller is 66
now 194 smaller is 67
now 195 smaller is 68
now 196 smaller is 69
now 197 smaller is 70
now 198 smaller is 71
now 199 smaller is 72
now 200 smaller is 73
虽然与(n+1)时全位二进制数不同之外
偶数情况都是相同的 所以共用一套公式
这样之后四种情况都已经求解 一般运算+long long即可
100pts
面对最后一个十分狗逼的数据点
我们不得不使用十分恶心的正解高精度计算
#pragma GCC optimize(3)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<string>
#include<queue>
#include<map>
#include<stack>
#include<list>
#include<set>
#include<deque>
#include<vector>
#include<ctime>
#define ll long long
#define inf 0x7fffffff
#define N 500008
#define IL inline
#define M 1008611
#define D double
#define ull unsigned long long
#define R register
using namespace std;
template<typename T>void read(T &a)
{
T x=0,f=1;char ch=getchar();
while(!isdigit(ch))
{
if(ch=='-')f=0;ch=getchar();
}
while(isdigit(ch))
{
x=(x<<1)+(x<<3)+ch-'0';ch=getchar();
}
a=f?x:-x;
}
/*-------------OI浣挎垜蹇箰-------------*/
int T;
struct Node{
int len,val[1010];
Node(){len=1;memset(val,0,sizeof(val));}
Node operator =(const char *s)//瀛楃涓?
{
len=strlen(s);
memset(val,0,sizeof val);
for(R int i=0;i<len;++i)
{
val[i]=s[len-i-1]-'0';
}
return *this;
}
Node operator =(const int& A)//璧嬪€?
{
char s[1010];
sprintf(s,"%d",A);
*this=s;
return *this;
}
Node operator +(const Node& A)const//鍔犳硶
{
Node B;
B.len=max(len,A.len);
for(R int i=0;i<len;++i)
B.val[i]=A.val[i]+val[i];
int k=0;
for(R int i=0;i<B.len;++i)
{
k+=B.val[i];
B.val[i]=k%10;
k/=10;
}
if(k>0)
{
B.val[B.len++]=k;
}
return B;
}
Node operator +=(const Node& A)//鍔犳硶
{
*this = *this + A;
return *this;
}
Node operator /=(const int& A)//浣庣簿搴﹂櫎娉?
{
int pos=0;
for(R int i=len-1;i>=0;--i)
{
pos=pos*10+val[i];
val[i]=pos/A;
pos%=A;
}
if(!val[len-1]) len--;
return *this;
}
Node operator *=(const int& A)//涔樻硶
{
int pos=0;
for(R int i=0;i<len;++i)
{
pos+=val[i]*A;
val[i]=pos%10;
pos/=10;
}
if(pos>0) {val[len]=pos;len++;}
return *this;
}
bool operator <=(const Node& A)const//灏忎簬绛変簬
{
if(len!=A.len) return len < A.len;
for(R int i=len-1;i>=0;--i)
{
if(val[i]<A.val[i]) return 1;
if(val[i]>A.val[i]) return 0;
}
return 1;
}
Node operator -=(const Node&A)
{
for(R int i=0;i<len;++i)
val[i]-=A.val[i];
for(R int i=0;i<len;++i)
if(val[i]<0) {val[i]+=10;val[i+1]--;}
while(len>1&&val[len-1]==0) len--;
return *this;
}
}ax,bx;
IL void put(Node ans)
{
for(R int i=ans.len-1;i>=0;--i) printf("%d",ans.val[i]);
printf("\n");
}
int main()
{
freopen("nim.in","r",stdin);
freopen("nim.out","w",stdout);
int T;
read(T);
while(T--)
{
char s[1010];
scanf("%s",s);
int len=strlen(s);
if((s[len-1]-'0')&1)
{
ax=s;bx=1;ax+=bx;
int k=s[len-1]-'0'+1;
if(len>1) k+=(s[len-2]-'0')*10;
if(!(k%4)) {printf("0\n");continue;}
ax/=2;put(ax);continue;
}
else
{
ax=s;bx=1;
while(bx<=ax) bx*=2;
bx/=2;
ax-=bx;bx=1;ax+=bx;
put(ax);
}
}
fclose(stdin);
fclose(stdout);
return 0;
}
-----------------------------------------下划线---------------------------------------
T2
【题目描述】
小林和亮亮是好朋友。小林有一个幸运数字 a,亮亮有一个幸运数字 b。一 个数字称之为“幸运数”当且仅当它只由 a 和 b 组成。小林称一个数为“魔数”, 当且仅当这个数各数位之和为“幸运数”,且这个数字本身也是“幸运数”。 举个例子:小林的幸运数字为 2,亮亮的幸运数字为 6,那么 23 不是“幸运 数”,而 26、 222 是“幸运数”。进一步, 222 是“魔数”(因为 2+2+2=6),而 26 不是“魔数”(因为 2+6=8)。 亮亮想要知道,有多少个 n 位的“魔数”(一个 n 位数不包含前导 0),由于 这个数字会很大,所以亮亮只关心这个数模 1000000007(10^9+7,是一个质数) 的结果。
【输入格式】
只有一行,包含三个整数: a、 b、 n。意义见题目描述。 【输出格式】 只有一个整数,表示 n 位的“魔数”的个数模 1000000007 的结果。
【样例输入】
2 6 3
【样例输出】
1
【样例解释】
两个幸运数字分别为 2 和 6,则位数为 3 的“魔数”只有 222 一个。
【数据规模】
对于 30%的数据: 1≤n≤5; 对于 60%的数据: 1≤n≤100; 对于 100%的数据: 1≤a<b≤9, 1≤n≤10^6。
题意分析
30pts
暴搜加玄学剪枝
60pts
我们考虑极端情况
我们可以发现枚举范围在[aaaaaaa,bbbbbbb]之间
但是如果(a×i+b×(n-i))是一个幸运数的话
无论怎么排列都是可以的
这就是一个组合数问题了 线性求组合数
100pts
都知道规律了 直接阶乘预处理就行了
为了防超时 先预处理 再使用费马小定理求解
CODE:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<string>
#include<ctime>
#include<cstdlib>
#include<map>
#include<stack>
#include<vector>
#define R register
#define IL inline
#define int long long
#define M 1008611
#define mod 1000000007
using namespace std;
template<typename T>void read(T &A)
{
T x=0,f=1;char ch=getchar();
while(!isdigit(ch))
{
if(ch=='-') f=0;ch=getchar();
}
while(isdigit(ch))
{
x=(x<<1)+(x<<3)+ch-'0';ch=getchar();
}
A=f ? x:-x;
}
int n,a,b,ans;
int fac[M];
IL bool check(int x)
{
while(x)
{
if(x%10!=a&&x%10!=b) return 0;
x/=10;
}
return 1;
}
IL int qpow(int x,int y)
{
int ans=1;
while(y)
{
if(y&1) ans=(ans*x)%mod;
x=(x*x)%mod;
y>>=1;
}
return ans%mod;
}
signed main()
{
freopen("number.in","r",stdin);
freopen("number.out","w",stdout);
read(a);read(b);read(n);fac[0]=1;
for(R int i=1;i<=n;++i) fac[i]=(fac[i-1]*i)%mod;
int res=n*b;ans+=check(res);
for(R int i=1;i<=n;++i)
{
res=res-b+a;
if(check(res)) ans=(ans+fac[n]*qpow(((fac[i]%mod)*(fac[n-i]%mod))%mod,mod-2)%mod)%mod;
}
printf("%lld\n",ans);
fclose(stdin);
fclose(stdout);
return 0;
}
-----------------------------------------下划线---------------------------------------
T3
【题目描述】
小林和亮亮刚入高中,首先就迎来了军训。这一天,他们班的同学正在教官 的指导下进行队列练习。 班上总共有 n 位同学,他们站成一排,然而,本该站成一条直线的他们却排 成了“一字长蛇阵”。用数学的语言来描述,如果把训练场看成一个平面直角坐 标系,第 i 名同学所在位置的横坐标是 i,而所有同学的纵坐标本该是 0(或者任 意一个相等的常量),这样就排成了一条直线。当然,由于同学们排的歪歪扭扭, 所以第 i 位同学的横坐标依然是 i,而纵坐标却成了 Yi (为了简化问题,我们假 设所有的坐标都是整数)。 对此,教官当然十分生气,因此他决定下命令调整队伍,使得所有人能够站 成一条直线(也即让所有的 Yi 相同)。教官的命令总共有三种: 1、除了某一个同学外,其余所有同学向前走一步(向前走一步可理解为 Yi 的 值加 1,下同); 2、除了某一个同学外,其余所有同学向前走两步; 3、除了某一个同学外,其余所有同学向前走五步。 教官希望他能以最少的命令次数使得所有同学站成一条直线,但是他不会算, 于是就让亮亮帮他来计算。亮亮虽然聪明,可是由于班上同学人数众多,他一下 子也解决不了这个问题,只能来寻求会编程的你的帮助,你能告诉亮亮答案吗?
【输入格式】
第一行有一个整数 n,表示班上共有 n 位同学。 第二行有 n 个整数,第 i 个整数 Yi 表示第 i 位同学初始位置的纵坐标。
【输出格式】
一个整数,表示最少的下达命令次数。
【样例输入】
4 1 1 2 6
【样例输出】
2
【样例解释】
一种方案是: 1 1 2 6 → 2 2 2 7 → 7 7 7 7
【数据规模】
对于 40%的数据, n <= 10, Yi <= 10; 对于 100%的数据, 1 <= n <= 100000, 0 <= Yi <= 1,000,000,000
题意分析
1.最后只要停留在一条线上就行 与具体位置无关
2.联想一下 NOIP[2016]蚯蚓
我们可以发现 一群人往前移动
根据相对运动 其实就是一个人向后移动
所以直接对每一个人单独考虑即可
40pts
利用背包的思路进行 DP
100pts
我们使用贪心的思路发现
最后的标准线就是最低的那个人
并且 能走5步就走5步 然后能走2步就走2步
然后再用1步去凑
CODE:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<string>
#include<ctime>
#include<cstdlib>
#include<map>
#include<stack>
#include<vector>
#define R register
#define IL inline
#define N 500008
#define int long long
using namespace std;
template<typename T>void read(T &A)
{
T x=0,f=1;char ch=getchar();
while(!isdigit(ch))
{
if(ch=='-') f=0;ch=getchar();
}
while(isdigit(ch))
{
x=(x<<1)+(x<<3)+ch-'0';ch=getchar();
}
A=f ? x:-x;
}
int n,minx,ans;
int num[N];
signed main()
{
freopen("queue.in","r",stdin);
freopen("queue.out","w",stdout);
read(n);
for(R int i=1;i<=n;++i) read(num[i]);
sort(num+1,num+n+1);minx=num[1];
for(R int i=1;i<=n;++i)
{
if(num[i]>minx)
{
int tmp=num[i]-minx;
ans+=tmp/5;tmp%=5;
ans+=tmp>>1;tmp&=1;
ans+=tmp;
}
}
printf("%lld\n",ans);
fclose(stdin);
fclose(stdout);
return 0;
}