1、sum of power
Description
Input
Input contains two integers n,m(1≤n≤1000,0≤m≤10).
Output
Output the answer in a single line.
Sample Input
10 0
Sample Output
10
题意:
求 的和求余 mod (1e9+7)
思路:
同余定理 + 快速幂
特别讨论了当:
m=0时,i^0 为1,结果为 n%mod
m=1时,i^1 为 i ,结果为 (n+1)*n/2
CODE:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <algorithm>
typedef long long LL;
const int mod=1e9+7;
using namespace std;
LL Pow(LL a,LL b)
{
LL ans=1;
while(b)
{
if(b&1)
ans=(ans%mod)*(a%mod)%mod;
a=(a%mod)*(a%mod)%mod;
b>>=1;
}
ans=ans%mod;
return ans;
}
int main()
{
LL n,m,nn;
LL sum=0;
scanf("%lld %lld",&n,&m);
if(m==0)
{
n=n%mod;
printf("%lld\n",n);
}
else if(m==1)
{
nn=(n+1)*n/2;
printf("%lld\n",nn);
}
else
{
for(LL i=1;i<=n;i++)
{
sum+=Pow(i,m);
sum=sum%mod;
}
printf("%lld\n",sum);
}
}
2、Parity check
Description
Input
Multiple test cases. Each test case is an integer n(0≤n≤101000 ) in a single line.
Output
For each test case, output the answer of f(n)mod2.
Sample Input
2
Sample Output
1
题意:
看题目翻译为 - 奇偶校验
利用斐波那契值 mod 2,问余数是多少
思路:
对 2 求余,结果是 0 或者 1,根据前几项得出规律,0 1 1 每三个一循环
比较坑的一点是:n的值太大,需要当作字符串输入
由于3个一循环,利用整数 3 的性质
一个数字的各位数字和能被3整除,则这个数字能被 3 整除
将字符串转化成 ASCII 码之和,再利用规律
CODE:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <algorithm>
typedef long long LL;
const int mod=1e9+7;
using namespace std;
char s[10002];
int main()
{
LL n,sum=0;
while(scanf("%s",s)!=EOF)
{
sum=0;
for(int i=0;i<strlen(s);i++)
sum=sum%3+(s[i]-'0')%3;
sum=sum%3;
if(sum==0)
printf("0\n");
else
printf("1\n");
}
}
3、quadratic equation
Description
With given integers a,b,c, you are asked to judge whether the following statement is true: "For any x, if , then x is an integer."
Input
The first line contains only one integer T(1≤T≤2000), which indicates the number of test cases.
For each test case, there is only one line containing three integers a,b,c(−5≤a,b,c≤5).
Output
or each test case, output “YES
” if the statement is true, or “NO
” if not.
Sample Input
3
1 4 4
0 0 1
1 3 1
Sample Output
YES
YES
NO
题意很简单,判断命题的正确性:
对于任意的x,如果
成立,则 x 是整数
这个命题是个蕴涵式:(如果,则)
判断命题真假:
p q p->q
0 0 1
0 1 1
1 0 0
1 1 1
(前提是0,结果是1)
思路:
1.a=b=c=0 时,对任意的 x 都满足,所以不成立
2. a = 0 时,不论b 、c 哪个为 0 ,都成立
3.不用考虑 b =0 的情况
例如: 3 0 1 ,此时 ?=0-4*3*1= -12 ,属于无解的情况,前提是 0, 则结果一定成立
若如果判断 b = 0 的情况,那么 3*x^2 + 1 =0 x^2 = - 1/3 无解则不成立
4.利用?判断成不成立
若 ? <0 则成立
否则需要判断 x1,x2 是否是整数
CODE:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <algorithm>
typedef long long LL;
const int mod=1e9+7;
using namespace std;
int main()
{
int t,a,b,c,ans,k,kk,p,pp;
int sum=0;
cin>>t;
while(t--)
{
cin>>a>>b>>c;
if(a==0&&b==0&&c==0)
{
cout<<"NO"<<endl;
continue;
}
if(a==0)
{
if(b==0) // 此时 c!=0,假设为 3=0,前提不成立,结果成立 (0 - 1)
{
cout<<"YES"<<endl;
continue;
}
if(c==0) // 此时 a=c=0,b!=0,若 b*x=0,则 x一定是0
{
cout<<"YES"<<endl;
continue;
}
k=(int)(-c/b);
if(b*k==-c) // b*x+c=0 ,需要判断 x 是否为整数
{
cout<<"YES"<<endl;
continue;
}
cout<<"NO"<<endl;
continue;
}
else
{
k=b*b-4*a*c;
if(k<0)
{
cout<<"YES"<<endl;
continue;
}
else
{
kk=sqrt(k);
if(kk*kk==k) // 判断? 开根号是否为整数
{
p=kk-b;
pp=-kk-b;
a*=2;
if(k>0)
{
if(pp%a==0&&p%a==0) // 判断两个解是否是整数
{
cout<<"YES"<<endl;
continue;
}
cout<<"NO"<<endl;
continue;
}
if(k==0)
{
if(-b%a==0)
{
cout<<"YES"<<endl;
continue;
}
cout<<"NO"<<endl;
continue;
}
}
else
{
cout<<"NO"<<endl;
continue;
}
}
}
}
}