CodeChef 2015年12月問題


CHEFST

(1)問題描述:

(2)要點:[1,m*(m+1)/2]內的每個數都可以表示為[1,m]的不同整數的和

(3)代碼:

#include <stdio.h>

int main()
{
unsigned int nCases = 0;scanf("%d",&nCases);
for(unsigned int iCases = 1;iCases <= nCases;++iCases)
{
unsigned long long n1 = 0,n2 = 0,m = 0;
//scanf("%I64d%I64d%I64d",&n1,&n2,&m);
scanf("%lld%lld%lld",&n1,&n2,&m);
if(n1 < n2) { n1 ^= n2;n2 ^= n1;n1 ^= n2; }
unsigned long long sum = (m+1)*m/2,ans = 0;
if(n2 < sum) ans = n1 - n2;
else ans = n1 - sum + n2 - sum;
printf("%llu\n",ans);
}
return 0;
}


PLANEDIV

(1)問題描述:

(2)要點:計算平行但不共線的最大的集合大小

(3)代碼:

#include <stdio.h>
#include <set>
#include <map>
using std::set;
using std::map;

// 平行但不共線
unsigned int gcd(unsigned int a,unsigned int b)
{
if(a < b) return gcd(b,a);
if(0 == b) return a;
if(1 == b) return 1;
return gcd(b,a%b);
}

template<class data_t>
class fraction_t
{
data_t numerator; // 分子
data_t denominator; // 分母
public:
fraction_t(const data_t& n,const data_t& d):numerator(n),denominator(d) { if(denominator < 0) { denominator = 0 - denominator;numerator = 0 - numerator; } } // 分母為非負
public:
bool operator<(const fraction_t& rhs)const { return numerator*rhs.denominator < denominator*rhs.numerator; }
bool operator==(const fraction_t& rhs)const { return numerator*rhs.denominator == denominator*rhs.numerator; }
bool operator<=(const fraction_t& rhs)const { return numerator*rhs.denominator <= denominator*rhs.numerator; }
};

int main()
{
typedef fraction_t<long long> Fraction;
typedef std::pair<int,int> Pair;
typedef std::pair<Pair,int> Truple;
unsigned int nCases = 0;scanf("%d",&nCases);
for(unsigned int iCases = 1;iCases <= nCases;++iCases)
{
unsigned int n = 0;scanf("%d",&n);
set<Truple> table;
map<Fraction,size_t> data;
unsigned int ans = 1;
for(unsigned int i = 0;i < n;++i)
{
int a = 0,b = 0,c = 0;
scanf("%d%d%d",&a,&b,&c);
int d = gcd(a>0?a:-a,gcd(b>0?b:-b,c>0?c:-c));
a /= d;b /= d;c /= d;
if(a < 0) { a = 0 - a;b = 0 - b;c = 0 - c; }
else if(0 == a && b < 0) { b = 0 - b;c = 0 - c; }
Truple u;u.first.first = a;u.first.second = b;u.second = c;
if(table.find(u) != table.end()) continue;
table.insert(u);
map<Fraction,size_t>::const_iterator itFind = data.find(Fraction(a,b));
unsigned int count = 0;
if(itFind != data.end()) count = itFind->second;
++ count;
if(count > ans) ans = count;
data[Fraction(a,b)] = count;
}
printf("%u\n",ans);
}
return 0;
}


ORACLCS

(1)問題描述:

(2)要點:若字符串中有x個a,y個b,則與m個a求LCS可以知道必然不超過x;同時若與m個b求LCS可以知道必然不超過y

(3)代碼:

#include <stdio.h>
#include <string.h>
#include <assert.h>
// 若字符串中有x個a,y個b,則與m個a求LCS可以知道必然不超過x;同時若與m個b求LCS可以知道必然不超過y
int main()
{
static const size_t buff_size = 1000;
char buff[buff_size] = { 0 };
unsigned int nCases = 0;scanf("%d",&nCases);
for(unsigned int iCases = 1;iCases <= nCases;++iCases)
{
unsigned int n = 0,ans = buff_size;scanf("%d",&n);
for(unsigned int i = 0;i < n;++i)
{
scanf("%s",buff);
unsigned int acount = 0,bcount = 0;
for(size_t k = 0,len = strlen(buff);k < len;++k)
{
acount += (buff[k] == 'a');
bcount += (buff[k] == 'b');
}
if(acount < ans) ans = acount;
if(bcount < ans) ans = bcount;
}
printf("%u\n",ans);
}
return 0;
}


(1)問題描述:

(2)要點:轉換為異或操作,+用1表示,-用0表示。問題轉換為在n個數中,有多少個不同的子集合,其異或結果為給定值v。即求異或方程組的解的個數。

(3)代碼:不包括高斯消元解異或方程組的模板代碼

#include <stdio.h>
#include <assert.h>
#include <string.h>

static unsigned int quick_modexp(unsigned int a,unsigned int module,unsigned long long x)
{
unsigned long long ret = 1,product = a%module;
for(;0 != x;x >>= 1)
{
if(x&1) ret = (product*ret)%module;
product = (product*product)%module;
}
return (unsigned int)(ret);
}

int main()
{
static const size_t bitcount = 10;
static const size_t maxn = 100000;
static const unsigned int module = 1000000007;
typedef gauss_jordan_elimination<int,long long> gje;
static const size_t buff_size = 100;
char buff[buff_size] = { 0 };
unsigned int* ans = new unsigned int[maxn];
unsigned int nCases = 0;scanf("%d",&nCases);
for(unsigned int iCases = 1;iCases <= nCases;++iCases)
{
unsigned int n = 0;scanf("%s%d",buff,&n);
gje slover(bitcount,n);
for(size_t i = 0;i < strlen(buff);++i)
{
slover(i,n) = (buff[i] == 'w');
}
memset(ans,0,sizeof(unsigned int)*maxn);
for(unsigned int i = 0;i < n;++i)
{
scanf("%s",buff);
for(size_t k = 0;k < strlen(buff);++k)
{
slover(k,i) = (buff[k] == '+');
}
}
unsigned int count = slover.xor_slove(ans,n);
unsigned int ret = 0;
if(count != gje::NO_ANSWER) ret = quick_modexp(2,module,count);
printf("%u\n",ret);
}
delete[] ans;
return 0;
}




注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com